JUnit Display Name Example - @DisplayName, @SuiteDisplayName and @UseTechnicalNames

1. Overview

In this post, we will learn how to display names for JUnit test classes, JUnit test methods, test suites, and display technical names of the test classes. 
We will discuss three display name annotations of JUnit 5
  • @DisplayName
  • @SuiteDisplayName
  • @UseTechnicalNames

1. Using @DisplayName

Test classes and test methods can declare custom display names — with spaces, special characters, and even emojis — that will be displayed by test runners and test reporting.

JUnit Display Names Example:

import static org.junit.jupiter.api.Assertions.assertEquals;

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

@DisplayName("@DisplayName annotation demo")
public class DisplayNameDemo {

	@Test
	@DisplayName("@DisplayName demo test")
	public void displayNameDemoTest() {
		assertEquals(3, 2 + 1);
	}
	
	@Test
	@DisplayName("@DisplayName demo with spaces")
	public void displayNameDemoWithSpaces() {
		assertEquals(3, 2 + 1);
	}
	
	@Test
	@DisplayName("@DisplayName demo with ╯°□°)╯ @!~ special characters")
	public void displayNameDemoWithSpecialCharacters() {
		assertEquals(3, 2 + 1);
	}
	
	@Test
	@DisplayName("@DisplayName demo with emojis - 😱")
	public void displayNameDemoWithEmojis() {
		assertEquals(3, 2 + 1);
	}
	
}

Output:


2. Using @SuiteDisplayName

To define a custom display name for the class run via @RunWith(JUnitPlatform.class) simply annotate the class with @SuiteDisplayName and provide a custom value.
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.SuiteDisplayName;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit 4 Suite Demo")
@SelectPackages("example")
public class JUnit4SuiteDemo {
}

3. @UseTechnicalNames

@UseTechnicalNames specifies that technical names should be used instead of display names when running a test suite on the JUnit Platform.

In JUnit 5, technical names are often referred to as the fully qualified name of a test class or the test method name itself.
@RunWith(JUnitPlatform.class)
@UseTechnicalNames
 class UseTechnicalNamesTest {

    @Test
    public void testPlaceOrderOK() {
        assertEquals(1, 1);
    }

    @Test
    public void testPlaceOrderWithAnonymousUser() {
        assertEquals(1, 1);
    }

    @Test
    public void testPlaceOrderWithAuthenticatedUser() {
        assertEquals(1, 1);
    }
}

Conclusion

In this post, we have learned the usage of three display name annotations of JUnit 5
  • @DisplayName
  • @SuiteDisplayName
  • @UseTechnicalNames

Comments