2017-04-27 9 views
0

次のコードを実行しようとしていますが、引数エラーが間違っています。JunitParamsRunnerテスト

package Homework7; 
import static junitparams.JUnitParamsRunner.$; 
import static org.junit.Assert.*; 
import junitparams.JUnitParamsRunner; 
import junitparams.Parameters; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 

@RunWith(JUnitParamsRunner.class) 

public class Problem3TestClass { 
    private Problem3Class p3class; 

    @SuppressWarnings("unused") 
    public static final Object[] Problem3TestClass(){ 
     return $(
//    Parameters are: (1, 2) 
//    1 = x,2 = y    
//    Test case 1 
       $(12223,1) 


       ); 
    } 
    @Before 
    public void setUp() { 
     p3class = new Problem3Class(); 
    } 

    @Test 
    @Parameters(method = "Problem3TestClass") 
    public void test(int[] x,int y) 
    { 
     assertEquals(y,p3class.countRepeated(x)); 
    } 

} 

マイcountRepeated方法は、私がここで間違って何をやっている、次のよう

public int countRepeated (int[] x) 

にinvocatedされますか?

答えて

1

はコメントによるとsource

で$方法

があるため、Javaはオブジェクトとプリミティブの VAR-引数を解決する方法のため、VAR-引数の配列を作成するために使用すべきではありません。

したがって、int []ではなくList of Integerを受け入れるようにテストメソッドを変更してみてください。 次のコードは動作するはずです。

@SuppressWarnings("unused") 
public static final Object[] Problem3TestClass() { 
    List<Integer> x = new ArrayList<Integer>(); 
    int y = 2; 
    return $(x, y); 
} 

@Test 
@Parameters(method = "Problem3TestClass") 
public void test(List<Integer> x, int y) { 
    // update countRepeated to accept List<Integer> or do some conversion here 
    assertEquals(y, p3class.countRepeated(x)); 
} 
0

リストタイプにも変更することをおすすめします。ただし、テストコードの実装が変更されないことがあります。 int []を使い続けるには、最後のパラメータとして保持することができます。

public static final Object[] Problem3TestClass(){ 
    return $(
     $(12223, new int[] {12223}), 
     $(6, new int[] {1,2,3}) 
    ); 
} 

とテストケース方法:以下

@Test 
@Parameters(method = "Problem3TestClass") 
public void test(int y, int[] x) 
{ 
    assertEquals(y, p3class.countRepeated(x)); 
} 

もリストタイプを使用するサンプルです。

public static final Object[] Problem3TestClass2(){ 
    return $(
     $(Arrays.asList(1,2,3), 6), 
     $(Arrays.asList(1,-4), -3) 
    ); 
} 

テストケース方法:テストで使用される2つの迅速な模擬方法添付

@Test 
@Parameters(method = "Problem3TestClass2") 
public void test2(List<Integer> x, int y) 
{ 
    assertEquals(y, p3class.countRepeated2(x)); 
} 

class Problem3Class{ 
    public int countRepeated (int[] x){ 
     return Arrays.stream(x).sum(); 
    } 
    public int countRepeated2 (List<Integer> x){ 
     return x.stream().reduce(0, (a, b) -> a+b); 
    } 
} 
関連する問題