2013-03-04 43 views
12

我正在創建基於JRE 6的Java應用程序。我使用JUnit 4生成參數化測試。我收到此錯誤:在包含註釋行Java JUnit參數化錯誤

The annotation @Parameterized.Parameters must define the attribute value

@Parameterized.Parameters 

下面是我認爲是與這一問題有關的代碼:

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

我設法找到一些與此相關的問題,如this。可悲的是,在我的情況下,他們沒有任何幫助。

我曾嘗試和沒有工作:

  • 取出從類聲明

  • 使用@Test標註添加測試功能 「延伸BasicCalculatorTest」

  • 導入org.junit.runners.Parameterized並使用@Parameters代替@ Parameterized.Parameters

我需要提到的是,我在另一個項目中使用了一個非常類似的實現(最值得注意的是annotations和testGenerator()),沒有任何問題。實施遵循在線提供的教程,如this,thisthis

任何幫助解決這個錯誤是非常感謝。

+2

'@ Parameterized.Parameters(value =/* here here * /)'該錯誤說明屬性'value'是強制性的。 – 2013-03-04 23:31:06

+0

@PaulBellora,這只是一個錯字,感謝您指出它,我已經糾正它,但問題仍然存在。 – 2013-03-04 23:32:27

+0

@BheshGurung,我知道它說,但我已經在另一個項目中使用它(value =/*這裏需要* /),它工作得很好。另外,我沒有鏈接的教程使用這個。 – 2013-03-04 23:34:13

回答

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

該代碼也出現此問題。 – 2013-03-05 15:49:59

+0

這對我來說運行良好使用jdk7和日食勝利7 x64 – 2013-03-05 17:27:57

+0

我忘了擴展基測試用例。我做了編輯並添加了輸出。 – 2013-03-05 17:31:25

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

您錯過了下面的導入我認爲。

​​