2.3. Display Names
Test classes and test methods can declare custom display names via @DisplayName
— with spaces, special characters, and even emojis — that will be displayed in test reports and by test runners and IDEs.
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("A special test case")
class DisplayNameDemo {
@Test
@DisplayName("Custom test name containing spaces")
void testWithDisplayNameContainingSpaces() {
}
@Test
@DisplayName("╯°□°)╯")
void testWithDisplayNameContainingSpecialCharacters() {
}
@Test
@DisplayName("😱")
void testWithDisplayNameContainingEmoji() {
}
}
2.3.1. Display Name Generators
JUnit Jupiter supports custom display name generators that can be configured via the @DisplayNameGeneration
annotation. Values provided via @DisplayName
annotations always take precedence over display names generated by a DisplayNameGenerator
.
Generators can be created by implementing DisplayNameGenerator
. Here are some default ones available in Jupiter:
DisplayNameGenerator | Behavior |
---|---|
| Matches the standard display name generation behavior in place since JUnit Jupiter 5.0 was released. |
| Removes trailing parentheses for methods with no parameters. |
| Replaces underscores with spaces. |
| Generates complete sentences by concatenating the names of the test and the enclosing classes. |
Note that for IndicativeSentences
, you can customize the separator and the underlying generator by using @IndicativeSentencesGeneration
as shown in the following example.
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.IndicativeSentencesGeneration;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class DisplayNameGeneratorDemo {
@Nested
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class A_year_is_not_supported {
@Test
void if_it_is_zero() {
}
@DisplayName("A negative value for year is not supported by the leap year computation.")
@ParameterizedTest(name = "For example, year {0} is not supported.")
@ValueSource(ints = { -1, -4 })
void if_it_is_negative(int year) {
}
}
@Nested
@IndicativeSentencesGeneration(separator = " -> ", generator = DisplayNameGenerator.ReplaceUnderscores.class)
class A_year_is_a_leap_year {
@Test
void if_it_is_divisible_by_4_but_not_by_100() {
}
@ParameterizedTest(name = "Year {0} is a leap year.")
@ValueSource(ints = { 2016, 2020, 2048 })
void if_it_is_one_of_the_following_years(int year) {
}
}
}
+-- DisplayNameGeneratorDemo [OK]
+-- A year is not supported [OK]
| +-- A negative value for year is not supported by the leap year computation. [OK]
| | +-- For example, year -1 is not supported. [OK]
| | '-- For example, year -4 is not supported. [OK]
| '-- if it is zero() [OK]
'-- A year is a leap year [OK]
+-- A year is a leap year -> if it is divisible by 4 but not by 100. [OK]
'-- A year is a leap year -> if it is one of the following years. [OK]
+-- Year 2016 is a leap year. [OK]
+-- Year 2020 is a leap year. [OK]
'-- Year 2048 is a leap year. [OK]
2.3.2. Setting the Default Display Name Generator
You can use the junit.jupiter.displayname.generator.default
configuration parameter to specify the fully qualified class name of the DisplayNameGenerator
you would like to use by default. Just like for display name generators configured via the @DisplayNameGeneration
annotation, the supplied class has to implement the DisplayNameGenerator
interface. The default display name generator will be used for all tests unless the @DisplayNameGeneration
annotation is present on an enclosing test class or test interface. Values provided via @DisplayName
annotations always take precedence over display names generated by a DisplayNameGenerator
.
For example, to use the ReplaceUnderscores
display name generator by default, you should set the configuration parameter to the corresponding fully qualified class name (e.g., in src/test/resources/junit-platform.properties
):
junit.jupiter.displayname.generator.default = \
org.junit.jupiter.api.DisplayNameGenerator$ReplaceUnderscores
Similarly, you can specify the fully qualified name of any custom class that implements DisplayNameGenerator
.
In summary, the display name for a test class or method is determined according to the following precedence rules:
value of the
@DisplayName
annotation, if presentby calling the
DisplayNameGenerator
specified in the@DisplayNameGeneration
annotation, if presentby calling the default
DisplayNameGenerator
configured via the configuration parameter, if presentby calling
org.junit.jupiter.api.DisplayNameGenerator.Standard