JUnit 5 Display Names

By | October 2, 2020

In JUnit 5, Test classes and test methods may declare custom display names using @DisplayName — with spaces, special characters, and even emojis — that will be displayed in test reports and by test runners and IDEs.

1.The default name of test classes and methods

The default name of test class and test methods are the same as a class or  test method name:

pom.xml 

Java  Class for that we have to write JUnit Tests: 

Test class

The output of the above project:

2. Customize the name of test class and test methods using @DisplayName Annotation

pom.xml

 Java  Class for that we have to write JUnit Tests: 

Test class

The output of the above project:

You can see the output of the above test display names is customized.

3. Display Name Generators

In JUnit Jupiter, we can create custom display name generators that can be configured via the @DisplayNameGeneration annotation. One thing you should note here that values specified via @DisplayName annotations always take precedence over display names generated by a DisplayNameGenerator

pom.xml

 Java  Class for that we have to write JUnit Tests: 

Test Classes:

MyUtilsTest1.java

MyUtilsTest2.java

The output of the above project:

4. Parameterized Tests  Customization display Name:

We can also customize the display name of Parameterized Tests  via the name attribute of the @ParameterizedTest, see the following example

pom.xml

Test Class

The output of the above project:

You May Also Like:

Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example

That’s all about JUnit 5 Display Names
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *