Display names

JUnit 4 identified tests basically with the name of the method annotated with @Test. This imposes a limitation on name tests, since these names are constrained by the way of declaring methods in Java.

To overcome this problem, Jupiter provides the ability of declaring a custom display name (different to the test name) for tests. This is done with the annotation @DisplayName. This annotation declares a custom display name for a test class or a test method. This name will be displayed by test runners and reporting tools, and it can contain spaces, special characters, and even emojis.

Take a look at the following example. We are annotating the test class, and also the three test methods declared inside the class with a custom test name using @DisplayName:

package io.github.bonigarcia;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("A special test case")
class DisplayNameTest {

@Test
@DisplayName("Custom test name containing spaces")
void testWithDisplayNameContainingSpaces() {
}

@Test
@DisplayName("(╯°Д°)╯")
void testWithDisplayNameContainingSpecialCharacters() {
}

@Test
@DisplayName("")
void testWithDisplayNameContainingEmoji() {
}

}

As a result, we see these labels when executing this test in a JUnit 5 compliant IDE. The following picture shows the execution of the example on IntelliJ 2016.2+:

Execution of a test case using @DisplayName in IntelliJ

On the other hand, the display name can be also seen in Eclipse 4.7 (Oxygen) or newer:

Execution of a test case using @DisplayName in Eclipse
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.150.123