如何在junit中编写字符串数组输入的测试用例?(junit的新内容)

时间:2017-12-30 15:51:41

标签: java maven junit

我是junit的新手并且尝试为我的程序编写2个测试用例,但由于它一直在说参数类型不匹配而无法执行它。 只需查看代码,如果可能请告诉我正确的解决方案。 我对简单变量及其工作尝试过相同的方法,但是对于字符串数组,它不起作用。

逻辑课程: 在这里,我试图在字符串中推送字符串的每个字母。

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();
}

这是我的Junit测试类:

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

}

}

记录错误消息:

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)

1 个答案:

答案 0 :(得分:1)

方法data()提供了String的列表。这意味着您的每个参数集都是一个String。不幸的是,您的测试类构造函数需要String数组而不是单个String。这就是你的测试失败的原因。