Introduction
@DisplayName helps make test names easy to read and understand, like giving a clear label to a test.
@DisplayName helps make test names easy to read and understand, like giving a clear label to a test.
@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.
@DisplayName("Check addition works correctly")
@Test
void testAdd() {
// test code
}@DisplayName("[x] Fails when input is null")
@Test
void testNullInput() {
// test code
}@DisplayName("Verify user login with valid credentials")
@Test
void userLoginTest() {
// test code
}This test class has two tests with clear, readable names shown in reports instead of method names.
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); } }
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.
@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.
@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