JUnit 5 lets you choose a default global DisplayNameGenerator for the entire test run via the junit-platform.properties file. This avoids adding @DisplayNameGeneration to every class while still producing readable names in IDEs and build reports.
Overview
- Place a file
src/test/resources/junit-platform.properties in your project.
- Set the property
junit.jupiter.displayname.generator.default to any generator class.
- Built-in options:
DisplayNameGenerator.Standard, Simple, ReplaceUnderscores, IndicativeSentences.
- Class-level
@DisplayNameGeneration and method-level @DisplayName still take precedence.
- Works with Maven Surefire, Gradle, and IDEs.
Examples
Project property file
src/main/resources/src/test/resources/junit-platform.propertiesjunit.jupiter.displayname.generator.default=org.junit.jupiter.api.DisplayNameGenerator$ReplaceUnderscores
Global generator in action (ReplaceUnderscores)
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class GlobalReplaceUnderscoresExampleTest {
@Test
void my_test_method() {
assertTrue(true);
}
@Test
void adds_two_numbers() {
assertEquals(4, 2 + 2);
}
}
mvn test -Dtest=GlobalReplaceUnderscoresExampleTest OutputD:\example-projects\junit-5\junit-5-test-display-name\junit-5-global-display-name-generator>mvn test -Dtest=GlobalReplaceUnderscoresExampleTest [INFO] Scanning for projects... [INFO] [INFO] -----< com.logicbig.example:junit-5-global-display-name-generator >----- [INFO] Building junit-5-global-display-name-generator 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-global-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-global-display-name-generator\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-global-display-name-generator --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-global-display-name-generator --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource from src\test\resources to target\test-classes [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-global-display-name-generator --- [INFO] Recompiling the module because of added or removed source files. [INFO] Compiling 3 source files with javac [debug target 25] to target\test-classes [INFO] [INFO] --- surefire:3.5.3:test (default-test) @ junit-5-global-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.GlobalReplaceUnderscoresExampleTest - 0.083 ss [INFO] | +-- [OK] my test method - 0.032 ss [INFO] | '-- [OK] adds two numbers - 0.011 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.752 s [INFO] Finished at: 2025-12-02T20:50:19+08:00 [INFO] ------------------------------------------------------------------------
Method @DisplayName overrides global
package com.logicbig.example;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class MethodDisplayNameOverridesGlobalTest {
@Test
@DisplayName("overridden method display name")
void my_test_method_with_custom_name() {
assertTrue(true);
}
@Test
void another_test_method_with_underscores() {
assertEquals(9, 4 + 5);
}
}
mvn test -Dtest=MethodDisplayNameOverridesGlobalTest OutputD:\example-projects\junit-5\junit-5-test-display-name\junit-5-global-display-name-generator>mvn test -Dtest=MethodDisplayNameOverridesGlobalTest [INFO] Scanning for projects... [INFO] [INFO] -----< com.logicbig.example:junit-5-global-display-name-generator >----- [INFO] Building junit-5-global-display-name-generator 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-global-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-global-display-name-generator\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-global-display-name-generator --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-global-display-name-generator --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource from src\test\resources to target\test-classes [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-global-display-name-generator --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.3:test (default-test) @ junit-5-global-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.MethodDisplayNameOverridesGlobalTest - 0.071 ss [INFO] | +-- [OK] overridden method display name - 0.036 ss [INFO] | '-- [OK] another test method with underscores - 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.915 s [INFO] Finished at: 2025-12-02T20:50:24+08:00 [INFO] ------------------------------------------------------------------------
Class-level @DisplayNameGeneration overrides global
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 ClassLevelGenerationOverridesGlobalTest {
@Test
void my_test_method_has_no_parentheses_in_simple() {
assertTrue(true);
}
@Test
void adds_two_numbers() {
assertEquals(7, 3 + 4);
}
}
mvn test -Dtest=ClassLevelGenerationOverridesGlobalTest OutputD:\example-projects\junit-5\junit-5-test-display-name\junit-5-global-display-name-generator>mvn test -Dtest=ClassLevelGenerationOverridesGlobalTest [INFO] Scanning for projects... [INFO] [INFO] -----< com.logicbig.example:junit-5-global-display-name-generator >----- [INFO] Building junit-5-global-display-name-generator 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-global-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-global-display-name-generator\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-global-display-name-generator --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-global-display-name-generator --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource from src\test\resources to target\test-classes [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-global-display-name-generator --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.3:test (default-test) @ junit-5-global-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.ClassLevelGenerationOverridesGlobalTest - 0.067 ss [INFO] | +-- [OK] my_test_method_has_no_parentheses_in_simple - 0.025 ss [INFO] | '-- [OK] adds_two_numbers - 0.020 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.110 s [INFO] Finished at: 2025-12-02T20:50:29+08:00 [INFO] ------------------------------------------------------------------------
Run everything
mvn test OutputD:\example-projects\junit-5\junit-5-test-display-name\junit-5-global-display-name-generator>mvn test [INFO] Scanning for projects... [INFO] [INFO] -----< com.logicbig.example:junit-5-global-display-name-generator >----- [INFO] Building junit-5-global-display-name-generator 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-global-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-global-display-name-generator\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-global-display-name-generator --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-global-display-name-generator --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource from src\test\resources to target\test-classes [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-global-display-name-generator --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.3:test (default-test) @ junit-5-global-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.ClassLevelGenerationOverridesGlobalTest - 0.056 ss [INFO] | +-- [OK] my_test_method_has_no_parentheses_in_simple - 0.025 ss [INFO] | '-- [OK] adds_two_numbers - 0.008 ss [INFO] +--com.logicbig.example.GlobalReplaceUnderscoresExampleTest - 0.005 ss [INFO] | +-- [OK] my test method - 0.001 ss [INFO] | '-- [OK] adds two numbers - 0.002 ss [INFO] +--com.logicbig.example.MethodDisplayNameOverridesGlobalTest - 0.005 ss [INFO] | +-- [OK] overridden method display name - 0.001 ss [INFO] | '-- [OK] another test method with underscores - 0.001 ss [INFO] [INFO] Results: [INFO] [INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.884 s [INFO] Finished at: 2025-12-02T20:50:34+08:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - junit-jupiter-api 6.0.1 (Module "junit-jupiter-api" of JUnit)
- junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.7.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 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
|