1

I'm developing a Java Spring project. I have little experience with JUnit, and encountered this problem when creating parameterized tests.

I'm making a sample test of the method ReportTableOperations.addDurations() which is basically a SUM of two long values.

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

    } 

 }

But I'm encountering the following errors when executing the test:

**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)

Anyone has any ideas in what could be causing this?

2
  • Is there a typo somewhere? Parameterized test needs constructor or field injection. Your thing looks kinda like it wants to do constructor injection, but there is no constructor, there is a method called AddDurationsTest. Commented Apr 9, 2018 at 18:25
  • Ha, I was looking in the wrong spot for this Exception and your question made it obvious where the error was. Commented May 30, 2022 at 22:08

2 Answers 2

2

Constructor name should be the same as class name and shouldn't declare returning type. You have class ReportTableOperationsTest and method which pretend to be constructor void AddDurationsTest . So just fix it like

private long value1, value2, expected;

public ReportTableOperationsTest(long value1, long value2, long expected) {
    this.value1 = value1;
    this.value2 = value2;
    this.expected = expected;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I was able to find an example of how to implement this in Kotlin for those Kotlin coders who come across here. The example is from here: https://gist.github.com/rossharper/8f6c3c169b6b5c23e12c

@RunWith(Parameterized::class)
class KotlinTest(val paramOne: Int, val paramTwo: String) {

    companion object {
        @JvmStatic
        @Parameterized.Parameters
        fun data() : Collection<Array<Any>> {
            return listOf(
                    arrayOf(1, "I"),         // First test:  (paramOne = 1, paramTwo = "I")
                    arrayOf(1999, "MCMXCIX") // Second test: (paramOne = 1999, paramTwo = "MCMXCIX")
            )
        }
    }

2 Comments

For Kotlin it's important to use listOf( arrayOf(...), ...) instead of listOf( listOf(...), ...). So use arrayOf inside listOf instead of listOf inside of listOf. For some reason, only the former works, and not the latter.
I tried to use a UByte as an argument, but it didn't work. I had to use a traditional Int and convert it later. I'm sharing this here as it may help someone someday.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.