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;
@Override
public 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());
}
}