Verify @DisplayName annotation shows readable test names in JUnit reports
Preconditions (2)
✅ 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
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.
Add two more test methods with different @DisplayName values and verify all display names appear correctly in the test report.
@DisplayName in JUnit tests?@DisplayName in a JUnit 5 test method?@Test
@DisplayName("Check if number is positive")
void isPositiveTest() {
assertTrue(5 > 0);
}@DisplayName:@Test
@DisplayName(Test addition)
void testAdd() {
assertEquals(4, 2 + 2);
}Test "add" method works. Which is the correct way to write the @DisplayName annotation?Pass the test and this pattern moves unknown