0
0
JUnittesting~5 mins

@DisplayName for readable names in JUnit

Choose your learning style9 modes available
Introduction

@DisplayName helps make test names easy to read and understand, like giving a clear label to a test.

When you want test names to be clear for anyone reading test reports.
When test method names are long or use code-style words that are hard to read.
When sharing test results with non-technical team members who need clear descriptions.
When you want to add spaces, special characters, or emojis to test names for clarity.
When organizing tests so reports show meaningful names instead of method names.
Syntax
JUnit
@DisplayName("Your readable test name here")
@Test
void testMethodName() {
    // test code
}

The annotation goes right above the test method.

The string inside @DisplayName can have spaces and symbols.

Examples
This test will show the name 'Check addition works correctly' in reports.
JUnit
@DisplayName("Check addition works correctly")
@Test
void testAdd() {
    // test code
}
You can use emojis or special characters to make test names stand out.
JUnit
@DisplayName("[x] Fails when input is null")
@Test
void testNullInput() {
    // test code
}
This makes the test name more descriptive than the method name.
JUnit
@DisplayName("Verify user login with valid credentials")
@Test
void userLoginTest() {
    // test code
}
Sample Program

This test class has two tests with clear, readable names shown in reports instead of method names.

JUnit
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {

    @DisplayName("Add two positive numbers")
    @Test
    void addPositiveNumbers() {
        int result = 2 + 3;
        assertEquals(5, result);
    }

    @DisplayName("Subtract smaller from larger number")
    @Test
    void subtractNumbers() {
        int result = 5 - 2;
        assertEquals(3, result);
    }
}
OutputSuccess
Important Notes

Use @DisplayName to make test reports easier to understand for everyone.

Keep display names short but descriptive.

Remember to import org.junit.jupiter.api.DisplayName.

Summary

@DisplayName gives tests clear, readable names.

It helps both technical and non-technical people understand test results.

Use it above test methods with a descriptive string.