如何在junit中编写字符串数组输入的测试用例?(junit的新增内容)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在junit中编写字符串数组输入的测试用例?(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)
答案
方法data()
提供了String
s列表。这意味着您的每个参数集都是单个String
。不幸的是,你的测试类构造函数需要一个String
数组而不是一个String
。这就是您的测试失败的原因。
以上是关于如何在junit中编写字符串数组输入的测试用例?(junit的新增内容)的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 junit 为 JPA REST API 编写测试用例?
如何使用 Junit 5 编写具有 System.exit() 的测试用例方法?