以下兩種情況之間的區別是:Junit @RunWith(參數化類)
a>變量'arr'的類型。在第一種情況下,它的類型是Integer [],而在第二種情況下,它的類型是int []。
b>我們可以從標記爲「// here」的地方看到,在情況1中它需要Integer [] [] []。而在案例2中它需要int [] []。
情況1和情況2都工作。所以我的問題來了:
爲什麼在測試數據是1維Integer數組時,Junit要求在case()方法中返回一個3維Integer數組。 我期望它應該是2維Integer數組,因爲在這種情況下2我在data()方法中返回一個2維int數組,並且它很容易理解它並且它可以工作。但是,當我試圖返回在數據2維整數數組()方法的情況下,1作爲
@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},
});
}
的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