Contents

Set display name Junit tests

Written by: David Vlijmincx

Setting the name of a test in JUnit

Changing the name of a test can be done by either changing the method name or by using the @DisplayName annotation. In the following example, you see the @DisplayName annotation on the class and the test method.

Placing the @DisplayName changes the name of the class. When you put @DisplayName on a test method it will change the name of the test method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("Test class name")
public class DisplayNameTest {

    @Test
    @DisplayName("Test method name")
    void testMethod(){
        // Test code here
    }
}

The names of the test will now look different in the console. When you run the previous tests in IntelliJ the result will look like the following:

img.png

Further reading

More about testing in Java: