Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JUnit参数化错误_Java_Unit Testing_Junit_Parameterized - Fatal编程技术网

Java JUnit参数化错误

Java JUnit参数化错误,java,unit-testing,junit,parameterized,Java,Unit Testing,Junit,Parameterized,我正在创建一个基于JRE 6的Java应用程序。我使用JUnit4生成参数化测试。我收到这个错误: The annotation @Parameterized.Parameters must define the attribute value 以下是我认为与此问题相关的代码: import static org.junit.Assert.assertEquals; import java.util.Arrays; import java.util.Collection; import or

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

The annotation @Parameterized.Parameters must define the attribute value 以下是我认为与此问题相关的代码:

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}
        });
    }
导入静态org.junit.Assert.assertEquals;
导入java.util.array;
导入java.util.Collection;
导入org.junit.Before;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.junit.runners.Parameterized;
导入calc.CalculatorException;
导入计算科学计算器;
@RunWith(参数化的.class)
公共类ScientificCalculatorTest扩展了基本计算器测试{
/**提供测试中计算器的科学特性的接口*/
私人科学计算器;
私人双a、b;
@以前
@凌驾
public void setUp()引发异常{
sciCalc=新的科学计算器();
//确保扩展计算器的基本功能
//还没坏。
theCalc=sciCalc;
}
/**
*构造函数。在每个测试上执行,并为数据集中的每对设置测试值。
*@param nr1测试对中的第一个数字。
*@param nr2测试对中的第二个数字。
*/
公共科学计算测试(双nr1,双nr2){
a=nr1;
b=nr2;
}
@参数化。参数化
公共静态集合testGenerator(){
返回Arrays.asList(新对象[][]{
//一般整数值|-/+组合
{  -100,  -100},
{  -100,   100},
{   100,  -100},
{   100,   100}
});
}
我设法找到了一些与此相关的问题,例如。不幸的是,在我的情况下,这些问题毫无帮助

我尝试过但没有成功的:

  • 从类声明中删除“ExtendedBasicCalcCalculator Test”

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

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

我需要指出的是,我在另一个项目中使用了非常类似的实现(最显著的是annotations和testGenerator()),没有任何问题

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

请尝试以下方法:

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

这一定是因为您扩展了BaseTestCase。我复制了您的代码,但没有从测试正确运行的基类进行扩展

尝试在安装程序中调用super.setUp()

例如


我想你错过了下面的要点

import org.junit.runners.Parameterized.Parameters;

我也遇到了同样的问题,我从一个测试超类扩展了我的测试,这个测试超类有一个init()方法,并用@org.junit.Before注释。
我在child类中实现了一个测试并运行了它,到目前为止一切正常。
然后我想使用参数化注释对不同的值重复测试,因此我使用@ParameterizedTest和@ValueSource,并运行了测试,但它不起作用,因为Super类中的初始化方法没有执行。
我覆盖了子类中的init()方法并调用了super.init(),但它不起作用。
有效的解决方案是在子类中的测试开始时调用super.init()
我认为这是一个兼容性问题,因为每次我在同一个测试类中混合JUnit4和JUnit5注释时,都会发生错误。

@Parameterized.Parameters(value=/*此处需要*/)
错误说明属性
是必需的。@PaulBellora,这只是一个输入错误,谢谢你指出,我已经纠正了,但问题仍然存在。@BheshGurung,我知道它这么说,但我在另一个项目中使用过它,但没有(value=/*此处需要*/)而且它工作得很好。而且,我链接的教程中没有一个使用这个。@VladSchnakovszki我这样问是因为我能够得到完全相同的代码,sans super class,在我的机器上工作。你能在“引用库”中发布你的项目的快照吗部分?我在win 7 X64上使用jdk7和eclipse时运行得很好。我忘了扩展基本测试用例。我进行了编辑并添加了输出。感谢您的回答。遗憾的是,我对您的解决方案给出的初始响应太短。如果我创建一个新项目,添加JUnit4库并添加代码,它会非常有效。问题是如果我d将代码添加到我遇到问题的项目中,它显示了相同的错误。我现在认为这可能是项目属性中的错误配置。从这里有什么想法吗?可能是旧项目中的有趣之处。尝试创建一个新项目并移动所有内容,或者删除并添加生成路径中的所有内容在旧项目上或将项目移动到新机器上。可能旧项目的类路径中有旧版本的JUnit?
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
@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;
}
import org.junit.runners.Parameterized.Parameters;