Java JUnit 参数化错误

2022-09-04 23:27:10

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

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

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

我尝试过但不起作用的内容:

  • 从类声明中删除“扩展 BasicCalculatorTest”

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

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

我需要提到的是,我在另一个项目中使用了非常相似的实现(最明显的是注释和testGenerator()),没有任何问题。该实现遵循在线提供的教程,例如

任何解决此错误的帮助都非常感谢。


答案 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

答案 2

我想你错过了下面的导入。

import org.junit.runners.Parameterized.Parameters;

推荐