Junit 入门(二)

来源:互联网 发布:switch网络一会b一会f 编辑:程序博客网 时间:2024/06/05 17:21
1.获取最大值类
package com.ebuair.junit;public class MaxValue {public int getMaxValue(int[] array) throws Exception{if(null == array || array.length == 0){throw new Exception("数组不能为空");}int maxValue = array[0];for(int i = 1; i < array.length; i++){if(maxValue < array[i]){maxValue = array[i];}}return maxValue;}}

2.测试类

package com.ebuair.junit;import junit.framework.Assert;import junit.framework.TestCase;public class TestMaxValue extends TestCase{private MaxValue maxValue = null;@Overridepublic void setUp() throws Exception {maxValue = new MaxValue();}public void testGetMaxValue(){int largetValue = 0;int[] array = new int[]{12,34,56,7,8};try {largetValue = maxValue.getMaxValue(array);} catch (Exception e) {Assert.fail("测试失败");}Assert.assertEquals(56, largetValue);}public void testGetMaxValueByNoValue(){Throwable throwable = null;int[] array = {};int largestValue = 0;try {largestValue = maxValue.getMaxValue(array);Assert.fail();} catch (Exception e) {throwable = e;}Assert.assertNotNull(throwable);Assert.assertEquals(Exception.class, throwable.getClass());Assert.assertEquals("数组不能为空", throwable.getMessage());}public void testGetMaxVauleByNull(){Throwable throwable = null;int[] array = null;int largestValue = 0;try {largestValue = maxValue.getMaxValue(array);Assert.fail();} catch (Exception e) {throwable = e;}Assert.assertNotNull(throwable);Assert.assertEquals(Exception.class, throwable.getClass());Assert.assertEquals("数组不能为空", throwable.getMessage());}}


0 0
原创粉丝点击