简体   繁体   中英

Junit Parameterized Test fail with java.lang.IllegalArgumentException: argument type mismatch

I created the following code which throws me " java.lang.IllegalArgumentException: argument type mismatch " exception. I believe it is caused by the incorrect return type Iterable. But I tried with Collection and Iterable, both throws me the argument type mismatch exception. Can someone please help? Thanks in advance. From the trace, it throws the exception after finishing the input() method.

@Parameterized.Parameters
public static Iterable<Object[]> input() {
    ArrayList<String>srcList = new ArrayList<String>();
    ArrayList<String>otherList = new ArrayList<String>();

    return Arrays.asList(new Object[][] {
        {srcList.add("https://www.test1.com"),20},
        {otherList.add("https://www.test2.com"),20}
    });
}

public WDDiffJunit2(ArrayList<String> url, int errCount) {
    this.url = url;
    this.errCount = errCount;
}

@Test
public void Test3() {     
    System.out.println("start test3");     
    loginPresenter.setModel(loginModel);
    loginPresenter.readProperties();
    loginPresenter.login();
    diffPresenter.setModel(diffModel);
    diffPresenter.setLoginModel(loginModel);
        assertEquals(errCount,diffPresenter.getExtractMaps(url,false).values().size());
}


public HashMap<String, String> getExtractMaps(ArrayList<String> urls, boolean isSource) {
     HashMap<String, String> a = new HashMap<String, String>();
     a.put("a","a");
     return a;
}

Stack Trace

java.lang.IllegalArgumentException: argument type mismatch at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTest(Parameterized.java:86) at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:236) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:233) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:23 1) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:24) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference. java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Replace ...

@Parameterized.Parameters
public static Iterable<Object[]> input() {
    ArrayList<String>srcList = new ArrayList<String>();
    ArrayList<String>otherList = new ArrayList<String>();

    return Arrays.asList(new Object[][] {
            {srcList.add("https://www.test1.com"),20},
            {otherList.add("https://www.test2.com"),20}
    });
}

... with:

@Parameterized.Parameters
public static Iterable<Object[]> input() {
    ArrayList<String> srcList = new ArrayList<String>();
    srcList.add("https://www.test1.com");
    ArrayList<String>otherList = new ArrayList<String>();
    otherList.add("https://www.test2.com");

    return Arrays.asList(new Object[][] {
            {srcList,20},
            {otherList,20}
    });
}

In its original form you were creating an Object[][] of boolean, int because srcList.add(...) returns a boolean . If you populate the srcList and otherList outside of the Object[][] initialiser then you'll end up with an Object[][] of the correct type: List, int .

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