2013-03-04 73 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

您错过了下面的导入我认为。

​​