In JUnit 5, you can add readable names to your tests using the @DisplayName annotation.
@DisplayName lets you show a friendly, human-readable test name in test reports and IDE test runners instead of relying only on the Java method name.
Example
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class CalculatorTest {
@Test
@DisplayName("Adding two positive numbers returns their sum")
void shouldAddTwoPositiveNumbers() {
int result = 2 + 3;
assertEquals(5, result);
}
}
The method name is still:
shouldAddTwoPositiveNumbers
But the test report can display:
Adding two positive numbers returns their sum
Add Display Names to Test Classes
You can also add @DisplayName to the test class itself:
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
@DisplayName("Password validation tests")
class PasswordValidatorTest {
@Test
@DisplayName("Valid password should be accepted")
void shouldAcceptValidPassword() {
assertTrue(true);
}
@Test
@DisplayName("Short password should be rejected")
void shouldRejectShortPassword() {
assertTrue(true);
}
}
Using Emojis or Symbols
JUnit 5 display names can include spaces, punctuation, and even emojis:
@Test
@DisplayName("✅ Valid email should pass validation")
void shouldAcceptValidEmail() {
assertTrue(true);
}
Use this carefully. Emojis can make reports more readable, but they may not be appropriate for every team or CI environment.
Display Names for Nested Tests
@DisplayName is especially useful with @Nested tests:
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@DisplayName("Shopping cart")
class ShoppingCartTest {
@Nested
@DisplayName("when adding items")
class AddingItems {
@Test
@DisplayName("updates the total item count")
void shouldUpdateItemCount() {
int itemCount = 1 + 2;
assertEquals(3, itemCount);
}
}
}
This can produce a readable test structure such as:
Shopping cart
└─ when adding items
└─ updates the total item count
Display Names for Parameterized Tests
For parameterized tests, you can customize each invocation name:
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertTrue;
class NumberTest {
@ParameterizedTest(name = "{0} should be positive")
@ValueSource(ints = {1, 5, 10})
void shouldBePositive(int number) {
assertTrue(number > 0);
}
}
This can display:
1 should be positive
5 should be positive
10 should be positive
You can also combine it with @DisplayName:
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertTrue;
class NumberTest {
@DisplayName("Positive number validation")
@ParameterizedTest(name = "Value {0} is positive")
@ValueSource(ints = {1, 5, 10})
void shouldBePositive(int number) {
assertTrue(number > 0);
}
}
Required Dependency
Make sure you are using JUnit Jupiter, which is JUnit 5:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.13.4</version>
<scope>test</scope>
</dependency>
For Gradle:
testImplementation 'org.junit.jupiter:junit-jupiter:5.13.4'
test {
useJUnitPlatform()
}
Summary
Use @DisplayName when you want test output to be more readable:
@Test
@DisplayName("User should be created when input is valid")
void shouldCreateUserWhenInputIsValid() {
}
The Java method name remains valid and searchable, while the test report shows a clearer description.
