JUnit参数化测试 - IllegalArgumentException:参数数量错误

时间:2018-04-09 18:14:11

标签: java spring testing junit parameters

我正在开发一个Java Spring项目。我对JUnit没什么经验,在创建参数化测试时遇到了这个问题。

我正在对ReportTableOperations.addDurations()方法进行样本测试,该方法基本上是两个长值的SUM。

@RunWith(Parameterized.class)
public class ReportTableOperationsTest {

    @Parameters
    public static Collection<Object[]> data() {
      return Arrays.asList(new Object[][] {
          {3600L, 3600L, 7200L},
          {2000L, 3600L, 5600L}
      });
    }

    private long value1, value2, expected;

    public void AddDurationsTest(long value1, long value2, long expected) {
        this.value1 = value1;
        this.value2 = value2;
        this.expected = expected;
    }


    @Test
    public void testAddDurations() throws Exception {

        assertThat(ReportTableOperations.addDurations(value1, value2), is(expected));

    } 

 }

但是我在执行测试时遇到以下错误:

**java.lang.IllegalArgumentException: wrong number of arguments**

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.createTestUsingConstructorInjection(BlockJUnit4ClassRunnerWithParameters.java:43)
at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.createTest(BlockJUnit4ClassRunnerWithParameters.java:38)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)

任何人都有可能导致这种情况的想法吗?

1 个答案:

答案 0 :(得分:2)

构造函数名称应与类名相同,不应声明返回类型。你有一个类ReportTableOperationsTest和假装是构造函数void AddDurationsTest的方法。所以就像

一样修复它
private long value1, value2, expected;

public ReportTableOperationsTest(long value1, long value2, long expected) {
    this.value1 = value1;
    this.value2 = value2;
    this.expected = expected;
}