javaexceptionjunitparams

junitparameter exception method should have no parameter


By using JUnitParameter for testing a methode I have an exception. My code is similar to lot of example on JUnitParameter:

    private Object parametersForTestSetDistanceFormated() {
    return new Object[][]{    
        {100,   "_1,__ km"}, 
        {100,   "1_,__ km"}, 
        {1100,  "11,__ km"}, 
        {110,   "1_,1_ km"}, 
        {111,   "1_,11 km"}};
}
/**
 * Test of setDistanceFormated method, of class ClientFormated.
 */
@Test
@Parameters
public void testSetDistanceFormated(final int exptDist, final String  distFormated) {
    System.out.println("setDistanceFormated");
    ClientFormated instance = new ClientFormated();             

    instance.setDistanceFormated(distFormated);
    assertEquals(exptDist, (long) instance.getDistance());
}

and then I obtain the following exception:

java.lang.RuntimeException: java.lang.Exception: Method testSetDistanceFormated should have no parameters
    at junitparams.JUnitParamsRunner.verifyMethodCanBeRunByStandardRunner(JUnitParamsRunner.java:429)
    at junitparams.JUnitParamsRunner.runChild(JUnitParamsRunner.java:420)
    at junitparams.JUnitParamsRunner.runChild(JUnitParamsRunner.java:386)
    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.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: java.lang.Exception: Method testSetDistanceFormated should have no parameters
    at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:76)
    at junitparams.JUnitParamsRunner.verifyMethodCanBeRunByStandardRunner(JUnitParamsRunner.java:427)
    ... 20 more

I haven't found related on the web...

My whole classe:

import com.entities.Client;
import junitparams.JUnitParamsRunner;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters;

@RunWith(JUnitParamsRunner.class)
public class ClientFormatedTest {

private Object parametersForTestSetDistanceFormated() {
    return new Object[][]{    
        {100,   "_1,__ km"}, 
        {100,   "1_,__ km"}, 
        {1100,  "11,__ km"}, 
        {110,   "1_,1_ km"}, 
        {111,   "1_,11 km"}};
}

/**
 * Test of setDistanceFormated method, of class ClientFormated.
 */
@Test
@Parameters
public void testSetDistanceFormated(final int exptDist, final String  distFormated) {
    System.out.println("setDistanceFormated");
    ClientFormated instance = new ClientFormated();             

    instance.setDistanceFormated(distFormated);
    assertEquals(exptDist, (long) instance.getDistance());
}

Solution

  • Imports need to be updated like :

    import com.entities.Client;
    import junitparams.JUnitParamsRunner;
    import junitparams.Parameters;
    import static org.junit.Assert.*;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    

    Now it works