JUnit4参数化测试
- 运行器
@RunWith(Parameterized.class)
public class ExampleTest {
}
- 增加属性字段
/**
* 测试方法需要的参数,类型同测试方法的参数;
* 如果有两个参数,就再加一个参数,
*/
private Integer parameter;
/**
* 测试方法返回的结果,类型同测试方法的结果;
*/
private String expected;
- 注入step2
方案一:
构造器注入:ExampleTest是我们编写测试类
public ExampleTest( int parameter, String expected) {
this. parameter = parameter;
this.expected = expected;
}
方案二:
注解注入:修饰符必须是public;
@Parameter
public int parameter;
@Parameter(1)
public String expected;
- 构造参数
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{1,"green"},
{2,"red"},
{null, null}
});
}
- 进行测试:testSPI.test(int parameter )被测试的方法
@Test
public void getById() {
String result = testSPI.test(parameter);
assertEquals(expected, result);
}