简体   繁体   中英

How to write test case for string array input in junit?(new to junit)

I am new to junit and trying out to write 2 test cases for my program, but unable to execute it as it keep saying argument type mismatch. Just have look on the code and if possible please let me know the proper solution. I have tried the same way for simple variable and its working, but for string array its not working.

Logic class : Here I am trying to push each letter of the string in the stack.

public class myClass{   

protected double myLogic(String[] exp) {
    Stack<Double> s = new Stack<Double>();
    String[] expArr = null;
    for (int j = 0; j < exp.length; j++) {
        expArr = exp[j].split("\\s");
    }
    for (int i = 0; i < expArr.length; i++) {

        if (expArr[i].matches("[0-9]+") || expArr[i].matches("[0-9].+")) {
            s.push(Double.parseDouble(String.valueOf(expArr[i])));
        }

}
 return s.pop();
}

and this is my Junit test class:

@RunWith(Parameterized.class)
public class MyTestCasess {

public String[] exp=null;
public Double[] pfResult = { 3, 3 };

public MyTestCasess(String[] exp1) {

    for(int i=0;i<exp1.length;i++){
        exp[i]=exp1[i];
    }
}

@Parameters
public static List<String> data() {
    String[] data = { "1 2 3", "6 2 3" };
    return Arrays.asList(data);
}

@Test
public void testMainCaller() {
    myClass objExp = new myClass();     
    Assert.assertEquals("Result", pfResult, objExp.myLogic(exp));

}

}

Log error message :

java.lang.IllegalArgumentException: argument type mismatch
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.BlockJUnit4ClassRunnerWithParameters.createTestUsingConstructorInjection(BlockJUnit4ClassRunnerWithParameters.java:43)
at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.createTest(BlockJUnit4ClassRunnerWithParameters.java:38)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
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:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

The method data() provides a list of String s. This means that each of your parameter sets is a single String . Unfortunately your test class constructor expects a String array instead of a single String . This is why your test is failing.

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