在JUnit 5中,我们可以使用@DisplayName
声明测试类和测试方法的自定义显示名称。
PS已通过JUnit 5.5.2测试
1. @DisplayName
1.1测试类和方法的默认名称。
DisplayNameTest.java
package com.mkyong.display;
import org.junit.jupiter.api.Test;
public class DisplayNameTest {
@Test
void test_spaces_ok() {
}
@Test
void test_spaces_fail() {
}
}
输出量
+-- JUnit Jupiter [OK]
| '-- DisplayNameTest [OK]
| +-- test_spaces_ok() [OK]
| '-- test_spaces_fail() [OK]
1.2 @DisplayName
DisplayNameCustomTest.java
package com.mkyong.display;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("I'm a Test Class")
public class DisplayNameCustomTest {
@Test
@DisplayName("Test with spaces, expected ok")
void test_spaces_ok() {
}
@DisplayName("Test with spaces, expected failed")
@Test
void test_spaces_fail() {
}
}
输出量
+-- JUnit Jupiter [OK]
| '-- I'm a Test Class [OK]
| +-- Test with spaces, expected ok [OK]
| '-- Test with spaces, expected failed [OK]
2.显示名称生成器
2.1我们还可以创建一个自定义显示名称生成器,并通过@DisplayNameGeneration
配置。
2.2此示例使用JUnit ReplaceUnderscores
生成器将下划线替换为空格。
DisplayNameGenerator1Test.java
package com.mkyong.display;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class DisplayNameGenerator1Test {
@Test
void test_spaces_ok() {
}
@Test
void test_spaces_fail() {
}
}
输出量
2.3我们可以扩展JUnit DisplayNameGenerator
以创建我们的自定义显示名称生成器。
DisplayNameGenerator2Test.java
package com.mkyong.display;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Collectors;
@DisplayNameGeneration(DisplayNameGenerator2Test.CustomDisplayNameGenerator.class)
public class DisplayNameGenerator2Test {
@Test
void test_spaces_ok() {
}
@Test
void test_spaces_fail() {
}
static class CustomDisplayNameGenerator extends DisplayNameGenerator.Standard {
@Override
public String generateDisplayNameForClass(Class<?> testClass) {
return "New Name for test class";
}
@Override
public String generateDisplayNameForNestedClass(Class<?> nestedClass) {
return super.generateDisplayNameForNestedClass(nestedClass);
}
@Override
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {
String name = testMethod.getName();
return Arrays.stream(name.split("_")).collect(Collectors.joining(" | "));
}
}
}
输出量
3.参数化测试
3.1对于参数化测试,我们可以通过@ParameterizedTest
的name属性声明自定义显示名称,请参见以下示例:
DisplayNameParamTest.java
package com.mkyong.display;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import static org.junit.jupiter.params.provider.Arguments.arguments;
public class DisplayNameParamTest {
@ParameterizedTest(name = "#{index} - Test with TimeUnit: {0}")
@EnumSource(value = TimeUnit.class, names = {"MINUTES", "SECONDS"})
void test_timeunit_ok(TimeUnit time) {
}
@ParameterizedTest(name = "#{index} - Test with {0} and {1}")
@MethodSource("argumentProvider")
void test_method_multi(String str, int length) {
}
static Stream<Arguments> argumentProvider() {
return Stream.of(
arguments("abc", 3),
arguments("lemon", 2)
);
}
}
输出量
下载源代码
$ git clone https://github.com/mkyong/junit-examples
$ cd junit5-examples
$检查src / test / java / com / mkyong / display / *。java