如何测试与参数测试下面的方法使用JUnitParamterized测试使用JUnit
public class Math {
public static int add(int a, int b) {
return a + b;
}
}
我想知道如何使用JUnit参数测试将实施以测试这种方法,当我想测试一下10个不同的参数。
如何测试与参数测试下面的方法使用JUnitParamterized测试使用JUnit
public class Math {
public static int add(int a, int b) {
return a + b;
}
}
我想知道如何使用JUnit参数测试将实施以测试这种方法,当我想测试一下10个不同的参数。
测试类必须具有注释@RunWith(Parameterized.class)
和函数返回一个Collection<Object[]>
应当标明@Parameters
和构造函数接收输入和预期输出(一个或多个)
API:http://junit.sourceforge.net/javadoc/org/junit/runners/Parameterized.html
@RunWith(Parameterized.class)
public class AddTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ { 0, 0, 0 }, { 1, 1 ,2},
{ 2, 1, 3 }, { 3, 2, 5 },
{ 4, 3, 7 }, { 5, 5, 10 },
{ 6, 8, 14 } } });
}
private int input1;
private int input2;
private int sum;
public AddTest(int input1, int input2, int sum) {
this.input1= input1;
this.input2= input2;
this.sum = sum;
}
@Test
public void test() {
assertEquals(sum, Math.Add(input1,input2));
}
}
最近我开始了zohhak项目。我相信它比@Parametrized要干净得多:
@TestWith({
"25 USD, 7",
"38 GBP, 2",
"null, 0"
})
public void testMethod(Money money, int anotherParameter) {
...
}