以下の2つのケースの違いは、Junit @RunWith(Parameterized.class)
>変数 'arr'のタイプです。最初のケースでは、その型はInteger []であり、2番目の型ではint []です。
b>ケース1では、Integer [] [] []が必要な場所を「//ここ」と表示しています。ケース2ではint [] []が必要です。
ケース1とケース2の両方が動作します。だから質問が来る:
なぜ私はテストデータが1次元の整数配列である場合、ケース1のdata()メソッドで3次元の整数配列を返す必要があるのですか? 2次元の整数配列でなければならないと思いました。ケース2では、data()メソッドで2次元のint配列が返され、理解しやすく、機能します。私は
@Parameterized.Parameters(name = "test with {index}")
public static Iterable<Integer[]> data() {
return Arrays.asList(new Integer[][]{
{3, 1, 4, 6, 7, 9},
{9, 8, 7, 6, 5, 4},
});
}
ように、ケース1のデータ()メソッドで2次元整数配列を返すようにしようとした場合にはJUnitは「:引数の間違った番号java.lang.IllegalArgumentExceptionが」を報告しました。あなたが理由を知っていれば私を助けてください。
私はJunitのドキュメントと他の多くのページを、満足のいく答えなしで検索しました。 助けてください。私のJunitバージョンは4.12です。
ケース1
@RunWith(Parameterized.class)
public class MyTest {
@Parameterized.Parameters(name = "test with {index}")
public static Iterable<Integer[][]> data() {
//here: My question is why it can not be Integer[][]
return Arrays.asList(new Integer[][][]{
{{3, 1, 4, 6, 7, 9}},
{{9, 8, 7, 6, 5, 4}},
});
}
private Integer[] arr;
public MyTest(Integer[] arr) {
this.arr = arr;
}
public methodTest(int[] arr) {
// ignore the code here
}
}
ケース2
@RunWith(Parameterized.class)
public class MyTest {
@Parameterized.Parameters(name = "test with {index}")
public static Iterable<int[]> data() {
//here int[][] works
return Arrays.asList(new int[][]{
{3, 1, 4, 6, 7, 9},
{9, 8, 7, 6, 5, 4},
}
private int[] arr;
public MyTest(int[] arr) {
this.arr = arr;
}
public methodTest(int[] arr) {
// ignore the code here
}
}
あなたの問題は何か明確ではありません。これらのケースのそれぞれについて期待された結果と実際の結果に関するコメントを追加してもよろしいですか?この問題を実証する[最小限の完全な例](http://stackoverflow.com/help/mcve)も有用でしょう。 – yeputons
両方のテストクラスの前に '@RunWith(Parameterized.class)'アノテーションがありますか? – yeputons
もう一度、あなたの問題は何ですか?これらのテストケースのいずれかが期待どおりのことをしていますか?問題を再現するためにプライベートフィールドが不可欠なのですか? JUnitのどのバージョンを使用していますか? JUnitは各パラメータの新しいテストクラスインスタンスを作成し、コンストラクタに渡されるのは – yeputons