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

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

Abstract: JUnit is a unit testing framework for the Java programming language. The JUnit 5 version has been redesigned to solve some particular problems of the previous versions. It comes with a lot of new annotations that make the life of the programmer easier and nicer. This article will put a few of them in practice.

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.


Catalin Tudose
Java and Web Technologies Expert
Nadal masz pytania?
Połącz sięz nami