工程目录:
参数化测试,SquareTest.java:
1 import static org.junit.Assert.*;
2
3 import java.util.Arrays;
4 import java.util.Collection;
5
6 import org.junit.Before;
7 import org.junit.Test;
8 import org.junit.runner.RunWith;
9 import org.junit.runners.Parameterized;
10 import org.junit.runners.Parameterized.Parameters;
11
12 @RunWith(Parameterized.class)
13 public class SquareTest {
14 private static Calculator calculator = new Calculator();
15 private int param;
16 private int result;
17
18
19 @Parameters
20 public static Collection data(){
21 return Arrays.asList(new Object[][]{
22 {2,4},
23 {0,0},
24 {-3,9},
25 });
26 }
27
28 //构造函数,对变量进行初始化
29 public SquareTest(int param,int result){
30 this.param = param;
31 this.result = result;
32 }
33
34 @Test
35 public void square(){
36 calculator.square(param);
37 assertEquals(result,calculator.getResult());
38 }
39
40 }
打包测试,AllCalculatorTests.java:
1 import org.junit.runner.RunWith;
2 import org.junit.runners.Suite;
3
4 @RunWith(Suite.class)
5 @Suite.SuiteClasses({
6 CalculatorTest.class,
7 SquareTest.class
8 })
9 public class AllCalculatorTests {
10
11 }
执行AllCalculatorTests.java的结果