Java JUnit参数化错误

Vla*_*zki 13 java junit unit-testing parameterized

我正在创建一个基于JRE 6的Java应用程序.我使用JUnit 4来生成参数化测试.我收到此错误:

The annotation @Parameterized.Parameters must define the attribute value

在包含注释的行上:

@Parameterized.Parameters
Run Code Online (Sandbox Code Playgroud)

以下是我认为与此问题相关的代码:

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}
        });
    }
Run Code Online (Sandbox Code Playgroud)

我设法找到一些相关的问题,比如这个问题.可悲的是,在我的情况下,他们没有任何帮助.

我尝试过但没有奏效:

  • 从类声明中删除"extends BasicCalculatorTest"

  • 添加使用@Test注释的测试函数

  • 导入org.junit.runners.Parameterized并使用@Parameters而不是@ Parameterized.Parameters

我需要提一下,我在另一个项目中使用了一个非常类似的实现(最值得注意的是注释和testGenerator())而没有任何问题.实现遵循在线提供的教程,例如this,this和this.

非常感谢任何有关解决此错误的帮助.

Ray*_*yek 2

尝试这个:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)