一、一个简单的测试
编写一个只有一种运算的计算器:
1 public class Calculator { 2 public static double divide(int dividend, int divisor) { 3 return dividend / divisor; 4 } 5 }
为这个方法编写测试:
1 public class CalculatorTest { 2 //允许误差 3 private static final double DELTA = 0.01; 4 5 @Test 6 public void testAdd() throws Exception { 7 assertEquals(3, Calculator.divide(9, 3), DELTA); 8 } 9 }
这个测试中使用9除以3测试了方法,但是我们希望使用多组数据进行测试,并且不需要编写多个方法,这时候可以使用JUnit的参数化测试。
二、参数化测试
在JUnit中,参数化测试有两种形式,第一种形式是构造函数形式,即JUnit遍历所提供的所有参数调用构造函数并执行测试方法:
1 //使用Parameterized Runner执行参数化测试 2 @RunWith(Parameterized.class) 3 public class CalculatorTest { 4 //允许误差 5 private static final double DELTA = 0.01; 6 7 //使用@Parameter标注public变量,JUnit会遍历每组参数进行注入 8 //注解中的整数参数表示注入参数组中的第几个参数 9 @Parameter(0) 10 public int dividend; 11 @Parameter(1) 12 public int divisor; 13 @Parameter(2) 14 public int result; 15 16 // 用@Parameterized.Parameters注解标注该方法是返回所有参数,被注解的方法必须返 17 // 回装载数组的Iterable对象,同时必须为public,static,当测试执行时,系统会遍历 18 // 每组参数(数组)调用构造函数并执行测试。 19 @Parameterized.Parameters 20 public static Iterable getParameters() { 21 return Arrays.asList(new Object[][]{ 22 {9, 3, 3}, {5, 1, 5}, {12, 4, 3} 23 }); 24 } 25 26 //当执行测试后,该方法会运行3次 27 @Test 28 public void testDivide() throws Exception { 29 assertEquals(result, Calculator.divide(dividend, divisor), DELTA); 30 } 31 }