2016-05-14 35 views
0

以下兩種情況之間的區別是: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 
    } 
} 
+0

不清楚你的問題是什麼。您是否介意在每種情況下添加一些關於預期和實際結果的評論? [最小完整示例](http://stackoverflow.com/help/mcve)演示該問題也很有用。 – yeputons

+0

在兩個測試類之前是否有'@RunWith(Parameterized.class)'註釋? – yeputons

+0

那麼,你又有什麼問題?做這些測試用例是否符合你的期望?是否有私人領域來重現問題?你使用什麼版本的JUnit?我有點擔心你傳遞給測試方法的額外參數 - 測試方法不應該接受任何參數,因爲JUnit爲每個參數創建一個新的測試類實例,只傳遞給構造函數 – yeputons

回答

1

數據本身應始終是相同的類型由構造預期的數據,而不是由測試。在data()的結構和數據本身之間,你也有點失落。我更喜歡下面的結構,明確表示我們所需要的:

@Parameters 
public static Collection<Object[]> data() 
{ 
    return Arrays.asList(
     new Object[][] 
     { 
      {/* case 1*/}, 
      {/* case 2*/}, 
     } 
    ); 
} 

也許這是不是最近的路,但你永遠不會迷路吧,即使是複雜的數據。這裏的每一行,除了/* case 1*//* case 1*/都不會改變。在頂層,我們有一個數組集合(的類型對象),我們通過提供一個2維數組對象來創建這個集合,每行都表示測試用例。

所以你案例1將成爲

@Parameterized.Parameters(name = "test with {index}") 
public static Collection<Object[]> data() { 
    return Arrays.asList(
     new Object[][] 
     { 
      {new Integer[]{3, 1, 4, 6, 7, 9}}, // here 
      {new Integer[]{9, 8, 7, 6, 5, 4}}, // here 
     } 
    ); 
} 

案例2將成爲

@Parameterized.Parameters(name = "test with {index}") 
public static Collection<Object[]> data() { 
    return Arrays.asList(
     new Object[][] 
     { 
      {new int[]{3, 1, 4, 6, 7, 9}}, // here 
      {new int[]{9, 8, 7, 6, 5, 4}}, // here 
     } 
    ); 
} 

你還需要解決幾個其他有問題行代碼,例如測試中缺少返回類型。下面是完整的可運行和測試通過的例子:

package x; 

import java.util.Arrays; 
import java.util.Collection; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.junit.runners.Parameterized; 

@RunWith(Parameterized.class) 
public class MyTest 
{ 
    @Parameterized.Parameters(name = "test with {index}") 
    public static Collection<Object[]> data() { 
     return Arrays.asList(
       new Object[][] 
       { 
        {new Integer[]{3, 1, 4, 6, 7, 9}}, // here 
        {new Integer[]{9, 8, 7, 6, 5, 4}}, // here 
     }); 
    } 

    private Integer[] arr; 

    public MyTest(Integer[] arr) { 
     this.arr = arr; 
    } 

    @Test 
    public void methodTest() { 
    // test some logic 
     System.out.println(arr.length); 
    } 
} 
+0

嗨Kiril S.,感謝您的幫助,如此詳細的情況下,這是一個很好的解決方法。 –

+0

@Kiril S.您不必將Object [] []數組轉換爲列表:'public static Object [] [] data()...'很好。 –

0

Integer版本需要比int版本的詳細尺寸的原因是,第一IterableIterable<Integer [][]>其中有一個比第二個,更Iterable<int[]>尺寸。

+0

沒有。這不是根本原因。 –