0
0
JUnittesting~15 mins

@DisplayName for readable names in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify @DisplayName annotation shows readable test names in JUnit reports
Preconditions (2)
Step 1: Create a test method with @Test annotation
Step 2: Add @DisplayName annotation with a readable name to the test method
Step 3: Run the test class using JUnit
Step 4: Observe the test report or IDE test runner output
✅ Expected Result: The test method should appear in the test report or IDE with the readable name specified in @DisplayName instead of the method name
Automation Requirements - JUnit 5
Assertions Needed:
Verify that the test runs successfully
Verify that the @DisplayName annotation is present on the test method
Best Practices:
Use @DisplayName to improve test readability in reports
Keep test method names descriptive but use @DisplayName for user-friendly names
Use assertions from org.junit.jupiter.api.Assertions
Automated Solution
JUnit
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class DisplayNameTest {

    @Test
    @DisplayName("Check if true is true - simple assertion")
    void testTrueIsTrue() {
        assertTrue(true, "True should be true");
    }

}

This test class uses JUnit 5.

The test method testTrueIsTrue is annotated with @Test to mark it as a test.

The @DisplayName annotation provides a readable name "Check if true is true - simple assertion" which will appear in test reports and IDE test runners instead of the method name.

The assertion assertTrue(true) is a simple check to ensure the test passes.

This setup helps testers and developers quickly understand the purpose of the test from reports.

Common Mistakes - 3 Pitfalls
Not using @DisplayName and relying only on method names
Using @DisplayName but with vague or unhelpful descriptions
Forgetting to import @DisplayName annotation
Bonus Challenge

Add two more test methods with different @DisplayName values and verify all display names appear correctly in the test report.

Show Hint