The annotation @DisplayName lets us assign a human-readable name to a test class or a test method. These names are shown in IDEs, build tool outputs, and reports, making test intent clearer than relying on Java identifiers. By default JUnit displays actual method name and simple class name in the reports .
Overview
- Can be applied at the class and/or method level.
- Supports any Unicode characters, spaces, punctuation and emojis (rendering depends on the tool).
- When present, it replaces the default display name derived from identifiers.
- Works with IDEs (IntelliJ IDEA, Eclipse), Maven Surefire, Gradle, and JUnit Platform reports.
- Use short, descriptive phrases that communicate the behavior under test.
Syntax
// Class level
@org.junit.jupiter.api.DisplayName("Readable suite name")
class MyTests { /*...*/ }
// Method level
@Test
@org.junit.jupiter.api.DisplayName("adds two numbers")
void addsTwoNumbers() { /*...*/ }
How it appears in reports
- IDE test trees show the provided display names instead of method identifiers.
- Console output from Maven/Gradle prints the display names where supported.
- When both class and method have
@DisplayName, the full path combines both.
Example
pom.xml<project .....> <modelVersion>4.0.0</modelVersion> <groupId>com.logicbig.example</groupId> <artifactId>junit-5-display-name-annotation</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>6.0.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.14.1</version> <configuration> <source>25</source> <target>25</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.5.0</version> <dependencies> <dependency> <groupId>me.fabriciorby</groupId> <artifactId>maven-surefire-junit5-tree-reporter</artifactId> <version>1.2.1</version> </dependency> </dependencies> <configuration> <statelessTestsetInfoReporter implementation= "org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoTreeReporter"> </statelessTestsetInfoReporter> </configuration> </plugin> </plugins> </build> </project>
In this example we have configured maven-surefire-plugin alone with maven-surefire-junit5-tree-reporter which display test report in a tree format when we run mvn test command.
Using @DisplayName
//Class-level DisplayName
@DisplayName("Math Operations Tests")
class SimpleMathTest {
// Method-level DisplayName (Standard sentence)
@Test
@DisplayName("Addition test")
void testAdd() {
int result = 2 + 3;
assertEquals(5, result);
}
// Method-level DisplayName
@Test
@DisplayName("Subtraction Test")
void testSubtract() {
int result = 5 - 10;
assertEquals(-5, result);
}
}
mvn test -Dtest=SimpleMathTest OutputD:\example-projects\junit-5\junit-5-test-display-name\junit-5-display-name-annotation>mvn test -Dtest=SimpleMathTest [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-display-name-annotation >-------- [INFO] Building junit-5-display-name-annotation 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-display-name-annotation --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-test-display-name\junit-5-display-name-annotation\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-display-name-annotation --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-display-name-annotation --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-test-display-name\junit-5-display-name-annotation\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-display-name-annotation --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-display-name-annotation --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] +--Math Operations Tests - 0.081 ss [INFO] | +-- [OK] Addition test - 0.039 ss [INFO] | '-- [OK] Subtraction Test - 0.013 ss [INFO] [INFO] Results: [INFO] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.895 s [INFO] Finished at: 2025-12-02T17:54:48+08:00 [INFO] ------------------------------------------------------------------------
Running test in Intellij
Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.0.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 5.0.0
- 5.0.1
- 5.0.2
- 5.0.3
- 5.1.0
- 5.1.1
- 5.2.0
- 5.3.0
- 5.3.1
- 5.3.2
- 5.4.0
- 5.4.1
- 5.4.2
- 5.5.0
- 5.5.1
- 5.5.2
- 5.6.0
- 5.6.1
- 5.6.2
- 5.6.3
- 5.7.0
- 5.7.1
- 5.7.2
- 5.8.0
- 5.8.1
- 5.8.2
- 5.9.0
- 5.9.1
- 5.9.2
- 5.9.3
- 5.10.0
- 5.10.1
- 5.10.2
- 5.10.3
- 5.10.4
- 5.10.5
- 5.11.0
- 5.11.1
- 5.11.2
- 5.11.3
- 5.11.4
- 5.12.0
- 5.12.1
- 5.12.2
- 5.13.0
- 5.13.1
- 5.13.2
- 5.13.3
- 5.13.4
- 5.14.0
- 5.14.1
- 6.0.0
- 6.0.1
Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|