一个TestCase测试实例
下面是一个数字功能类,它提供了求最大值函数和求最小值函数:
package com.zj.c01;
public class NumberTool {
public static int getMax(int[] arr) {
int max = Integer.MIN_VALUE;
if (arr.length == 0)
throw new RuntimeException("Empty list");
for (int index = 0; index < arr.length; index++) {
if (arr[index] > max)
max = arr[index];
}
return max;
}
public static int getMin(int[] arr) {
int min = Integer.MAX_VALUE;
if (arr.length == 0)
throw new RuntimeException("Empty list");
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min)
min = arr[i];
}
return min;
}
} |
下面针对求最大值函数编写测试用例:
1.简单测试:[7,8,9]->9;
2.位序测试:[9,8,7] ->9;[7,9,8] ->9;[8,7,9] ->9;
3.重复值测试:[9,7,9,8] ->9;
4.单值测试:[1]->1;
5.负值测试:[-7,-8,-9]->-7;
6.空值测试:[]->抛出异常;
测试类NumberToolTest:
package com.zj.c01;
import junit.framework.TestCase;
public class NumberToolTest extends TestCase {
public NumberToolTest(String name) {
super(name);
}
public void testSimple() {
assertEquals(9, NumberTool.getMax(new int[] { 7, 8, 9 }));
}
public void testOrder() {
assertEquals(9, NumberTool.getMax(new int[] { 9, 8, 7 }));
assertEquals(9, NumberTool.getMax(new int[] { 7, 9, 8 }));
assertEquals(9, NumberTool.getMax(new int[] { 8, 7, 9 }));
}
public void testDups() {
assertEquals(9, NumberTool.getMax(new int[] { 9, 7, 9, 8 }));
}
public void testOne() {
assertEquals(1, NumberTool.getMax(new int[] { 1 }));
}
public void testNegitave() {
assertEquals(-7, NumberTool.getMax(new int[] { -7, -8, -9 }));
}
public void testEmpty() {
try {
NumberTool.getMax(new int[] {});
fail("Should have thrown an exception");
} catch (RuntimeException e) {
assertTrue(true);
}
}
} |
使用Eclipse-Run As JUnit Test
相关阅读: