使用参数化Junit测试获取运行时异常

时间:2013-08-10 07:19:07

标签: java junit4

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

/ * ** * *** 忽略 * ** * ** * ** * ** * *** / 添加更多细节 添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节添加更多细节细节

2 个答案:

答案 0 :(得分:2)

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

另外,你有一个

public void testHashMapParams(HashMap cont) 

此方法/函数与“testHashMapParams”类名相同。

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

答案 1 :(得分:1)

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

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