0

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)
4
  • Please add the error / log message as well to the post... Commented Dec 30, 2017 at 17:22
  • There are no error log, the code is working fine when the string input is taken from the console. But by using Junit test cases it fails. I may be not implementing the Junit test cases properly. The error in Junit tab is "Argument mismatch" Commented Dec 30, 2017 at 19:53
  • Please try to run the build on command line via mvn clean package... Commented Dec 30, 2017 at 20:16
  • I have tried but found no luck yet..! I have added the error log for your reference, please see and let me know if you have any suggestions. Thanks :) Commented Dec 31, 2017 at 14:34

1 Answer 1

1

The method data() provides a list of Strings. 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.

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

1 Comment

So you are saying, I should declare two separate string variables and then proceed with the test cases?

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.