2013-03-04 9 views
12

JRE 6に基づいてJavaアプリケーションを作成しています。JUnit 4を使用してパラメータ化されたテストを生成します。私が管理しJava JUnitパラメータ化エラー

import static org.junit.Assert.assertEquals; 

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

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

import calc.CalculatorException; 
import calc.ScientificCalculator; 

@RunWith(Parameterized.class) 
public class ScientificCalculatorTest extends BasicCalculatorTest{ 

    /** Provides an interface to the scientific features of the calculator under test */ 
    private ScientificCalculator sciCalc; 
    private double a, b; 


    @Before 
    @Override 
    public void setUp() throws Exception { 
     sciCalc = new ScientificCalculator(); 
     //Make sure that the basic functionality of the extended calculator 
     //hasn't been broken. 
     theCalc = sciCalc; 
    } 

    /** 
    * Constructor. Is executed on each test and sets the test values to each pair in the data sets. 
    * @param nr1 the first number in the tested pair. 
    * @param nr2 the second number in the tested pair. 
    */ 
    public ScientificCalculatorTest(double nr1, double nr2){ 
     a = nr1; 
     b = nr2; 
    } 


    @Parameterized.Parameters 
    public static Collection<Object[]> testGenerator() { 
     return Arrays.asList(new Object[][] { 
       //General integer values | -/+ combinations 
       { -100, -100}, 
       { -100, 100}, 
       { 100, -100}, 
       { 100, 100} 
     }); 
    } 

:私はこの問題に関連すると信じているコードである

@Parameterized.Parameters 
以下

:注釈を含む行の

The annotation @Parameterized.Parameters must define the attribute value

:私はこのエラーが発生しますthisのような関連性の高い質問を見つけます。残念ながら、私の状況では彼らは役に立たない。

私が試みていると動作しませんでした何:クラス宣言

  • @Testアノテーションを使用する追加のテスト機能から "BasicCalculatorTestを拡張し、" 削除

    • @ Parameterized.Parametersの代わりにorg.junit.runners.Parameterizedをインポートして@Parametersを使用する

    私は非常によく似た実装(主に注釈とtestGenerator())を問題なく他のプロジェクトで使用したことに言及する必要があります。実装は、this,thisthisなど、オンラインで入手できるチュートリアルに従います。

    このエラーを解決するための助けがあれば幸いです。

  • +2

    '@ Parameterized.Parameters(value =/* required here * /)'というエラーは、属性 'value'は必須です。 –

    +0

    @PaulBellora、それはちょうどタイプミスでした、それを指摘してくれてありがとう、私はそれを修正しましたが、問題はまだ残っています。 –

    +0

    @BheshGurung、私はそれは言うが、私は別のプロジェクト(値=/*ここに必要* /)でそれを使用して、それはうまくいきました。また、私がリンクしているチュートリアルのどれもこれを使用していません。 –

    答えて

    1

    はこれを試してみてください。

    import java.util.Arrays; 
    import java.util.Collection; 
    
    import org.junit.Test; 
    import org.junit.runner.RunWith; 
    import org.junit.runners.Parameterized; 
    import org.junit.runners.Parameterized.Parameters; 
    public class So15213068 { 
        public static class BaseTestCase { 
         @Test public void test() { 
          System.out.println("base class test"); 
         } 
        } 
        @RunWith(Parameterized.class) public static class TestCase extends BaseTestCase { 
         public TestCase(Double nr1,Double nr2) { 
          //super(nr1,nr2); 
          this.nr1=nr1; 
          this.nr2=nr2; 
         } 
         @Test public void test2() { 
          System.out.println("subclass test "+nr1+" "+nr2); 
         } 
         @Parameters public static Collection<Object[]> testGenerator() { 
          return Arrays.asList(new Object[][]{{-100.,-100.},{-100.,100.},{100.,-100.},{100.,100.}}); 
         } 
         double nr1,nr2; 
        } 
    } 
    

    出力:

    subclass test -100.0 -100.0 
    base class test 
    subclass test -100.0 100.0 
    base class test 
    subclass test 100.0 -100.0 
    base class test 
    subclass test 100.0 100.0 
    base class test 
    
    +0

    このコードでも問題が発生します。 –

    +0

    これはjdk7とeclipseを使って7 x64のwinでうまく動作します。 –

    +0

    基本テストケースを拡張するのを忘れました。私はその編集を行い、出力を追加しました。 –

    0

    あなたがBaseTestCaseを拡張するので、それがなければなりません。テストが正しく実行されている基底クラスから拡張せずにコードをコピーしました。

    例えば、あなたのセットアップでsuper.setUp()を呼び出す

    をお試しください

    @Before 
    @Override 
    public void setUp() throws Exception { 
        super.setUp(); 
        sciCalc = new ScientificCalculator(); 
        //Make sure that the basic functionality of the extended calculator 
        //hasn't been broken. 
        theCalc = sciCalc; 
    } 
    
    1

    私は以下のことを考えています。

    ​​
    関連する問題