1
@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

2
  • Please show the full stack trace. Commented Aug 10, 2013 at 7:22
  • Thnx! edited to show the full stack trace Commented Aug 10, 2013 at 12:18

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Done that. Still causing the same exception and pardon for the typo in the constructor argument name duely changed it to: HashMap dataToTest; String cont; public void TestHashMapParams(HashMap dataToTest) { this.dataToTest=dataToTest; }
"public void TestHashMapParams(HashMap dataToTest) { this.dataToTest=dataToTest; }" -- This is actually not the way to define the constructor. Try public TestHashMapParams(HashMap dataToTest) {this.dataTotTest=dataToTest;}. Notice that I don't have a void token on the constructor.
I don't think there is an issue with the constructor. Prolly Junit Paramterized.class has the answer to it.
1

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

i.e.

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

2 Comments

Maybe you can replace parameterizing with this. Instead of using @RunWith(value=Parameterized.class) and adding a constructor and the public static Collection giveMe() You can just use the @BeforeClass
@Aguragorn a method annotated with BeforeClass must be static. Can you please update your answer?

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.