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)

上面的参数化测试意味着运行两次给出运行时异常!!虽然它运行了两次,但出现了上述异常

这里可能出了什么问题?

完整的堆栈跟踪

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)

/ * ** * ***忽略下面* ** * ** * ** * ** * *** / 添加更多详细信息 添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息添加更多详细信息

4

2 回答 2

2

您似乎有一个带有“testHashMapParams”的类,为了约定,它应该是“TestHashMapParams”。

另外,你有一个

public void testHashMapParams(HashMap cont) 

此方法/功能与您的“testHashMapParams”类名相同。

如果这不是一个错字,也许你应该先重命名/重构类名,然后我们会看看这是否会导致问题。

于 2013-08-10T07:50:19.493 回答
1

除了更正您的构造函数之外,您可能还想使用@BeforeClass注解

IE

@BeforeClass
public void init(){
    //initialize dataToTest here
}
于 2013-08-12T06:14:14.870 回答