有没有办法使用 @TestCaseName
注释打印数组的内容(作为测试参数之一传递)?
我的意思是我要这个测试用例
private static final File JAVA_DIRECTORY = get("src/main/java/").toFile();
private static final String[] NOT_ACCEPTABLE_IN_JAVA_DIRECTORY = {"groovy", "css", "html"};
public Object[] filesNotAcceptedInDirectories() {
return $(
$(JAVA_DIRECTORY, NOT_ACCEPTABLE_IN_JAVA_DIRECTORY)
);
}
@Test
@Parameters(method = "filesNotAcceptedInDirectories")
@TestCaseName("Verify that no files with extensions {1} are present in directory {0}")
public void verifyFilesArePlacedInAppropriateDirectories(File directory, String[] unacceptableFiles) {
assertThat(listFiles(directory, unacceptableFiles, true)).isEmpty();
}
显示为
Verify that no files with extensions [groovy, css, html] are present in directory src\main\java
我目前得到的是
Verify that no files with extensions String[] are present in directory src\main\java
最佳答案
目前看来是不可能的。将参数字符串化的部分是junitparams.internal.Utils#addParamToResult
:
private static String addParamToResult(String result, Object param) {
if (param == null)
result += "null";
else {
try {
tryFindingOverridenToString(param);
result += param.toString();
} catch (Exception e) {
result += param.getClass().getSimpleName();
}
}
return result;
}
private static void tryFindingOverridenToString(Object param)
throws NoSuchMethodException {
final Method toString = param.getClass().getMethod("toString");
if (toString.getDeclaringClass().equals(Object.class)) {
throw new NoSuchMethodException();
}
}
您可以看到,只有在参数覆盖 Object#toString
时才会使用覆盖 toString
的情况,否则它只是类的简单名称,在 a 的情况下字符串数组是 String[]
。
关于java - 从作为参数传递的数组创建测试用例名称(使用 JUnitParams),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34507375/