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.

  1. import org.junit.jupiter.api.DisplayName;
  2. import org.junit.jupiter.api.Test;
  3. @DisplayName("A special test case")
  4. class DisplayNameDemo {
  5. @Test
  6. @DisplayName("Custom test name containing spaces")
  7. void testWithDisplayNameContainingSpaces() {
  8. }
  9. @Test
  10. @DisplayName("╯°□°)╯")
  11. void testWithDisplayNameContainingSpecialCharacters() {
  12. }
  13. @Test
  14. @DisplayName("😱")
  15. void testWithDisplayNameContainingEmoji() {
  16. }
  17. }

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:

DisplayNameGeneratorBehavior

Standard

Matches the standard display name generation behavior in place since JUnit Jupiter 5.0 was released.

Simple

Removes trailing parentheses for methods with no parameters.

ReplaceUnderscores

Replaces underscores with spaces.

IndicativeSentences

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.

  1. import org.junit.jupiter.api.DisplayName;
  2. import org.junit.jupiter.api.DisplayNameGeneration;
  3. import org.junit.jupiter.api.DisplayNameGenerator;
  4. import org.junit.jupiter.api.IndicativeSentencesGeneration;
  5. import org.junit.jupiter.api.Nested;
  6. import org.junit.jupiter.api.Test;
  7. import org.junit.jupiter.params.ParameterizedTest;
  8. import org.junit.jupiter.params.provider.ValueSource;
  9. class DisplayNameGeneratorDemo {
  10. @Nested
  11. @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
  12. class A_year_is_not_supported {
  13. @Test
  14. void if_it_is_zero() {
  15. }
  16. @DisplayName("A negative value for year is not supported by the leap year computation.")
  17. @ParameterizedTest(name = "For example, year {0} is not supported.")
  18. @ValueSource(ints = { -1, -4 })
  19. void if_it_is_negative(int year) {
  20. }
  21. }
  22. @Nested
  23. @IndicativeSentencesGeneration(separator = " -> ", generator = DisplayNameGenerator.ReplaceUnderscores.class)
  24. class A_year_is_a_leap_year {
  25. @Test
  26. void if_it_is_divisible_by_4_but_not_by_100() {
  27. }
  28. @ParameterizedTest(name = "Year {0} is a leap year.")
  29. @ValueSource(ints = { 2016, 2020, 2048 })
  30. void if_it_is_one_of_the_following_years(int year) {
  31. }
  32. }
  33. }
  1. +-- DisplayNameGeneratorDemo [OK]
  2. +-- A year is not supported [OK]
  3. | +-- A negative value for year is not supported by the leap year computation. [OK]
  4. | | +-- For example, year -1 is not supported. [OK]
  5. | | '-- For example, year -4 is not supported. [OK]
  6. | '-- if it is zero() [OK]
  7. '-- A year is a leap year [OK]
  8. +-- A year is a leap year -> if it is divisible by 4 but not by 100. [OK]
  9. '-- A year is a leap year -> if it is one of the following years. [OK]
  10. +-- Year 2016 is a leap year. [OK]
  11. +-- Year 2020 is a leap year. [OK]
  12. '-- 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):

  1. junit.jupiter.displayname.generator.default = \
  2. 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:

  1. value of the @DisplayName annotation, if present

  2. by calling the DisplayNameGenerator specified in the @DisplayNameGeneration annotation, if present

  3. by calling the default DisplayNameGenerator configured via the configuration parameter, if present

  4. by calling org.junit.jupiter.api.DisplayNameGenerator.Standard