JUnitでパラメーター化されたテストを使用して次のメソッドをテストするにはどうすればよいですか?
public class Math {
public static int add(int a, int b) {
return a + b;
}
}
10個の異なる引数でテストしたい場合、このメソッドをテストするためにJunitを使用したパラメーター化されたテストがどのように実装されるかを知りたいです。
JUnitでパラメーター化されたテストを使用して次のメソッドをテストするにはどうすればよいですか?
public class Math {
public static int add(int a, int b) {
return a + b;
}
}
10個の異なる引数でテストしたい場合、このメソッドをテストするためにJunitを使用したパラメーター化されたテストがどのように実装されるかを知りたいです。
テスト クラスには、注釈@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) {
...
}