从作为参数传递的数组创建测试用例名称(使用 JUnitParams)
Create test case name (using JUnitParams) from an array passed as a parameter
有没有办法使用 @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
,否则它只是 class 简单名称,在字符串数组的情况下是 String[]
.
待新版本(1.0.5)发布后即可 - https://github.com/Pragmatists/JUnitParams/issues/70
有没有办法使用 @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
,否则它只是 class 简单名称,在字符串数组的情况下是 String[]
.
待新版本(1.0.5)发布后即可 - https://github.com/Pragmatists/JUnitParams/issues/70