Newly-Introduced JUnit 5 Annotations and Classes. @DisplayName annotation

Newly-Introduced JUnit 5 Annotations and Classes. @DisplayName annotation

 

1. The @DisplayName annotation

 

The @DisplayName annotation can be used over classes and test methods. It helps to declare our own display name for the annotated test class or test method. Typically, it’s used for test reporting in IDEs and build tools. The string argument of the @DisplayName annotation may contain spaces, special characters, and even emojis.

 

Listing 1 demonstrates the usage of the @DisplayName annotation through the class displayname.DisplayNameTest. The name to be displayed is usually a full phrase to provide significant information about the purpose of the test.

 

@DisplayName annotation.JPG

 

The result of the execution of these tests from the IntelliJ IDE looks like in figure 1:

DisplayNameTest in IntelliJ.png

Figure 1 Running the DisplayNameTest in IntelliJ.

 

The execution of the example from listing 1 does the following:

  1. It shows the display name applied to the entire class (1).
  2. Then, we see that we may apply a usual text display name (2).
  3. We may also include an emoji (3). The test without an associated display name shows the method name.

Interested in JUnit?

Check out our trainings
Tudose, Florin-Catalin , Java Champion; Java Chapter Lead; Author at Pluralsight and Manning

Tudose, Florin-Catalin Tudose author linkedin

Java Champion; Java Chapter Lead; Author at Pluralsight and Manning

Tudose, Florin-Catalin , Java Champion; Java Chapter Lead; Author at Pluralsight and Manning

Tudose, Florin-Catalin Tudose author linkedin

Java Champion; Java Chapter Lead; Author at Pluralsight and Manning