2016-05-14 1 views
0

以下の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 
    } 
} 
+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は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 
     } 
    ); 
} 

なります

@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 
     } 
    ); 
} 

ケースになりますテストのリターンタイプの欠如。ここでは、完全な実行可能と通過テストの例を示します。

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バージョンよりも多くの寸法を必要な理由は、最初のIterableが第二1、Iterable<int[]>より1つの多い寸法を有するIterable<Integer [][]>であるということです。

+0

番号。これは根本的な理由ではありません。 –

関連する問題