考虑使用 JUnit 4 和 JUnitParams 的这个测试类:
import static junitparams.JUnitParamsRunner.$;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(JUnitParamsRunner.class)
public class JUnitParamsExample {
private int[] getIntArray() {
int array[] = new int[2];
array[0] = 1;
array[1] = 2;
return array;
}
public Object getInts() {
return $($(getIntArray()));
}
@Parameters(method = "getInts")
@Test
public void testIntArray(int... values) {
//
}
private String[] getStringArray() {
String array[] = new String[2];
array[0] = "a";
array[1] = "b";
return array;
}
public Object getStrings() {
return $($(getStringArray()));
}
@Parameters(method = "getStrings")
@Test
public void testStringArray(String... values) {
//
}
}
测试方法testIntArray
运行良好,但testStringArray
没有。错误输出为:
java.lang.IllegalArgumentException: Cannot parse parameters. Did you use , as column separator? a
at junitparams.internal.InvokeParameterisedMethod.castParamsFromString(InvokeParameterisedMethod.java:51)
at junitparams.internal.InvokeParameterisedMethod.<init>(InvokeParameterisedMethod.java:35)
at junitparams.internal.ParameterisedTestClassRunner.buildMethodInvoker(ParameterisedTestClassRunner.java:121)
at junitparams.internal.ParameterisedTestClassRunner.parameterisedMethodInvoker(ParameterisedTestClassRunner.java:115)
at junitparams.JUnitParamsRunner.methodInvoker(JUnitParamsRunner.java:425)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:251)
at junitparams.JUnitParamsRunner.runChild(JUnitParamsRunner.java:405)
at junitparams.JUnitParamsRunner.runChild(JUnitParamsRunner.java:383)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.IllegalArgumentException: Parameter type cannot be handled! Only primitive types and Strings can be used.
at junitparams.internal.InvokeParameterisedMethod.castParameterDirectly(InvokeParameterisedMethod.java:171)
at junitparams.internal.InvokeParameterisedMethod.castAllParametersToProperTypes(InvokeParameterisedMethod.java:122)
at junitparams.internal.InvokeParameterisedMethod.castParamsUsingConverters(InvokeParameterisedMethod.java:101)
at junitparams.internal.InvokeParameterisedMethod.castParamsFromString(InvokeParameterisedMethod.java:49)
我想知道为什么 int 数组有效而字符串数组无效。有没有办法使用 junitparams 将字符串数组传递给 varargs 测试方法?
编辑:我想写的是这样的:
public Object getStrings() {
return $($("Hello", "world"),
$("Me", "You"),
$("Dog", "Cat"));
}
这样所有内部 $-entries 都被传递给 varargs 参数。