简体   繁体   中英

Getting a runtime Exception with parameterized Junit test

@RunWith(value=Parameterized.class)
public class TestHashMapParams {

    HashMap dataToTest;
    String cont;
    public void TestHashMapParams(HashMap dataToTest)
    {
        this.dataToTest=cont;
    }


    @Test
    public void hashTest()
    {
        System.out.println(dataToTest.get("Key"));
    }


    @Parameters
    public static Collection giveMe()
    {
        ArrayList<HashMap[]> ab=new ArrayList<HashMap[]>();
        HashMap[] bc1=new HashMap[1];
        HashMap[] bc2=new HashMap[1];

        HashMap a1=new HashMap<>();
        HashMap a2=new HashMap<>();

        //HashMap arr[] = {new HashMap(), new HashMap()};
        ArrayList<HashMap> itrtor1=new ArrayList<HashMap>();
        ArrayList<HashMap> itrtor2=new ArrayList<HashMap>();

        a1.put("Key", "val1");
        a2.put("Key","val2");

        itrtor1.add(a1);
        itrtor2.add(a2);

        bc1=itrtor1.toArray(new HashMap[itrtor1.size()]);
        bc2=itrtor2.toArray(new HashMap[itrtor2.size()]);

        ab.add(bc1);
        ab.add(bc2);

        return ab;
    }
}



java.lang.IllegalArgumentException: wrong number of arguments
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

The above parameterized test meant to run two times giving a Runtime exception!! Though it runs two times but the above exception appears

What might be going wrong here??

Full stack trace

java.lang.IllegalArgumentException: wrong number of arguments
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTestUsingConstructorInjection(Parameterized.java:186)
    at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTest(Parameterized.java:181)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:244)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

/ * ** * *** ignore below * ** * ** * ** * ** * *** / adding more details adding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more detailsadding more details

you seem to have a class with "testHashMapParams " which should be "TestHashMapParams" for convention's sake.

Plus, you have a

public void testHashMapParams(HashMap cont) 

This method/function is identical with your "testHashMapParams " class name.

If this is not a typo, maybe you should rename/refactor the class name first then we'll see if that causes the problem.

Aside from correcting your constructor, you may also want to make use of the @BeforeClass annotation

ie

@BeforeClass
public void init(){
    //initialize dataToTest here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM