gpt4 book ai didi

Java JUnit 参数化错误

转载 作者:搜寻专家 更新时间:2023-10-30 21:12:42 29 4
gpt4 key购买 nike

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

The annotation @Parameterized.Parameters must define the attribute value

on the line containing the annotation:

@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 .可悲的是,在我的情况下,他们没有帮助。

我尝试过但没有奏效的:

  • 从类声明中删除“extends BasicCalculatorTest”

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

  • 导入 org.junit.runners.Parameterized 并使用 @Parameters 代替 @Parameterized.Parameters

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

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

最佳答案

试试这个:

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

关于Java JUnit 参数化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15213068/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com