JUnit 5 provides @DisplayNameGeneration to automatically derive readable names for test classes and methods. Instead of writing @DisplayName on each test, you can choose a generator strategy once per class (or globally) and have display names produced consistently in IDEs and build reports.
Overview
- Annotate a test class with
@DisplayNameGeneration(GeneratorClass.class).
- Built-in generators include:
DisplayNameGenerator.Standard, Simple, ReplaceUnderscores, and IndicativeSentences.
- Generators transform identifiers (camelCase, underscores) into human-readable display names.
- Method-level
@DisplayName still takes precedence over generated names.
Quick syntax
// at class level
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class MyTests { /*...*/ }
Built-in generators
@DisplayNameGeneration(DisplayNameGenerator.Standard.class)
@DisplayNameGeneration(DisplayNameGenerator.Simple.class)
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@DisplayNameGeneration(DisplayNameGenerator.IndicativeSentences.class)
Examples
Using built-in generators
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class NoDisplayGeneratorExampleTest {
@Test
void testAddition() {
assertEquals(4, 2 + 2);
}
@Test
void my_test_method() {
assertTrue(true);
}
}
mvn test -Dtest=NoDisplayGeneratorExampleTest.java OutputD:\example-projects\junit-5\junit-5-test-display-name\junit-5-display-name-generator>mvn test -Dtest=NoDisplayGeneratorExampleTest.java [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-display-name-generator >--------- [INFO] Building junit-5-display-name-generator 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-display-name-generator --- [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-generator\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-display-name-generator --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-display-name-generator --- [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-generator\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-display-name-generator --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-display-name-generator --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] +--com.logicbig.example.NoBuiltInGeneratorExampleTest - 0.065 ss [INFO] | +-- [OK] my_test_method - 0.032 ss [INFO] | '-- [OK] testAddition - 0.005 ss [INFO] [INFO] Results: [INFO] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.014 s [INFO] Finished at: 2025-12-02T19:23:48+08:00 [INFO] ------------------------------------------------------------------------
package com.logicbig.example;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@DisplayNameGeneration(DisplayNameGenerator.Simple.class)
class SimpleDisplayGeneratorExampleTest {
@Test
void testAddition() {
assertEquals(4, 2 + 2);
}
@Test
void my_test_method() {
assertTrue(true);
}
}
mvn test -Dtest=SimpleDisplayGeneratorExampleTest OutputD:\example-projects\junit-5\junit-5-test-display-name\junit-5-display-name-generator>mvn test -Dtest=SimpleDisplayGeneratorExampleTest [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-display-name-generator >--------- [INFO] Building junit-5-display-name-generator 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-display-name-generator --- [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-generator\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-display-name-generator --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-display-name-generator --- [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-generator\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-display-name-generator --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-display-name-generator --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] +--com.logicbig.example.SimpleBuiltInGeneratorExampleTest - 0.069 ss [INFO] | +-- [OK] my_test_method - 0.035 ss [INFO] | '-- [OK] testAddition - 0.007 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.956 s [INFO] Finished at: 2025-12-02T19:38:32+08:00 [INFO] ------------------------------------------------------------------------
package com.logicbig.example;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@DisplayNameGeneration(DisplayNameGenerator.Standard.class)
class StandardDisplayGeneratorExampleTest {
@Test
void testAddition() {
assertEquals(4, 2 + 2);
}
@Test
void my_test_method() {
assertTrue(true);
}
}
mvn test -Dtest=StandardDisplayGeneratorExampleTest OutputD:\example-projects\junit-5\junit-5-test-display-name\junit-5-display-name-generator>mvn test -Dtest=StandardDisplayGeneratorExampleTest [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-display-name-generator >--------- [INFO] Building junit-5-display-name-generator 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-display-name-generator --- [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-generator\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-display-name-generator --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-display-name-generator --- [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-generator\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-display-name-generator --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.3:test (default-test) @ junit-5-display-name-generator --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] +--com.logicbig.example.StandardDisplayGeneratorExampleTest - 0.067 ss [INFO] | +-- [OK] my_test_method - 0.033 ss [INFO] | '-- [OK] testAddition - 0.008 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.793 s [INFO] Finished at: 2025-12-02T20:34:30+08:00 [INFO] ------------------------------------------------------------------------
Standard generator retains method names with parentheses for methods without parameters (see all output in Intellij bellow, in mvn output it somehow doesn't show parentheses with standard generator), while the Simple generator removes these trailing parentheses for a cleaner name. The Standard generator is the default
package com.logicbig.example;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class ReplaceUnderscoresDisplayGeneratorExampleTest {
@Test
void testAddition() {
assertEquals(4, 2 + 2);
}
@Test
void my_test_method() {
assertTrue(true);
}
}
mvn test -Dtest=ReplaceUnderscoresDisplayGeneratorExampleTest OutputD:\example-projects\junit-5\junit-5-test-display-name\junit-5-display-name-generator>mvn test -Dtest=ReplaceUnderscoresDisplayGeneratorExampleTest [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-display-name-generator >--------- [INFO] Building junit-5-display-name-generator 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-display-name-generator --- [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-generator\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-display-name-generator --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-display-name-generator --- [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-generator\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-display-name-generator --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-display-name-generator --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] +--com.logicbig.example.ReplaceUnderscoresDisplayGeneratorExampleTest - 0.066 ss [INFO] | +-- [OK] should return correct value - 0.032 ss [INFO] | '-- [OK] testAddition - 0.009 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.840 s [INFO] Finished at: 2025-12-02T19:57:58+08:00 [INFO] ------------------------------------------------------------------------
package com.logicbig.example;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@DisplayNameGeneration(DisplayNameGenerator.IndicativeSentences.class)
@DisplayName("DefaultIndicativeTest")
class DefaultIndicativeSentencesBuiltInGeneratorExampleTest {
@Test
void testAddition() {
assertEquals(4, 2 + 2);
}
@Test
void my_test_method() {
assertTrue(true);
}
}
mvn test -Dtest=DefaultIndicativeSentencesBuiltInGeneratorExampleTest OutputD:\example-projects\junit-5\junit-5-test-display-name\junit-5-display-name-generator>mvn test -Dtest=DefaultIndicativeSentencesBuiltInGeneratorExampleTest [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-display-name-generator >--------- [INFO] Building junit-5-display-name-generator 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-display-name-generator --- [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-generator\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-display-name-generator --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-display-name-generator --- [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-generator\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-display-name-generator --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-display-name-generator --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] +--MyTest - 0.098 ss [INFO] | +-- [OK] MyTest > my test method - 0.044 ss [INFO] | '-- [OK] MyTest > testAddition - 0.012 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.850 s [INFO] Finished at: 2025-12-02T19:25:25+08:00 [INFO] ------------------------------------------------------------------------
package com.logicbig.example;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
@DisplayNameGeneration(DisplayNameGenerator.IndicativeSentences.class)
@IndicativeSentencesGeneration(separator = " > ", generator = DisplayNameGenerator.ReplaceUnderscores.class)
@DisplayName("IndicativeAnnotationTest")
class IndicativeSentencesBuiltInGeneratorExampleTest {
@Test
void testAddition() {
assertEquals(4, 2 + 2);
}
@Test
void my_test_method() {
assertTrue(true);
}
}
mvn test -Dtest=IndicativeSentencesBuiltInGeneratorExampleTest OutputD:\example-projects\junit-5\junit-5-test-display-name\junit-5-display-name-generator>mvn test -Dtest=IndicativeSentencesBuiltInGeneratorExampleTest [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-display-name-generator >--------- [INFO] Building junit-5-display-name-generator 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-display-name-generator --- [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-generator\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-display-name-generator --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-display-name-generator --- [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-generator\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-display-name-generator --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-display-name-generator --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] +--MyTest - 0.066 ss [INFO] | +-- [OK] MyTest, my_test_method() - 0.039 ss [INFO] | '-- [OK] MyTest, testAddition() - 0.008 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.571 s [INFO] Finished at: 2025-12-02T19:25:47+08:00 [INFO] ------------------------------------------------------------------------
Running all together
mvn test OutputD:\example-projects\junit-5\junit-5-test-display-name\junit-5-display-name-generator>mvn test [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-display-name-generator >--------- [INFO] Building junit-5-display-name-generator 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-display-name-generator --- [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-generator\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-display-name-generator --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-display-name-generator --- [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-generator\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-display-name-generator --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.3:test (default-test) @ junit-5-display-name-generator --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] +--DefaultIndicativeTest - 0.062 ss [INFO] | +-- [OK] DefaultIndicativeTest, my_test_method() - 0.028 ss [INFO] | '-- [OK] DefaultIndicativeTest, testAddition() - 0.008 ss [INFO] +--IndicativeAnnotationTest - 0.005 ss [INFO] | +-- [OK] IndicativeAnnotationTest > my test method - 0.001 ss [INFO] | '-- [OK] IndicativeAnnotationTest > testAddition - 0.002 ss [INFO] +--com.logicbig.example.NoDisplayGeneratorExampleTest - 0.005 ss [INFO] | +-- [OK] my_test_method - 0.001 ss [INFO] | '-- [OK] testAddition - 0.001 ss [INFO] +--com.logicbig.example.ReplaceUnderscoresDisplayGeneratorExampleTest - 0.004 ss [INFO] | +-- [OK] my test method - 0 ss [INFO] | '-- [OK] testAddition - 0.001 ss [INFO] +--com.logicbig.example.SimpleDisplayGeneratorExampleTest - 0.003 ss [INFO] | +-- [OK] my_test_method - 0.001 ss [INFO] | '-- [OK] testAddition - 0 ss [INFO] +--com.logicbig.example.StandardDisplayGeneratorExampleTest - 0.004 ss [INFO] | +-- [OK] my_test_method - 0.001 ss [INFO] | '-- [OK] testAddition - 0.002 ss [INFO] [INFO] Results: [INFO] [INFO] Tests run: 12, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.832 s [INFO] Finished at: 2025-12-02T20:29:35+08:00 [INFO] ------------------------------------------------------------------------
Running all in Intellij
|