JUnit 5 provides configuration parameters to set default ordering strategies globally, eliminating the need to annotate every test class with @TestMethodOrder or @TestClassOrder. This is particularly useful for establishing project-wide ordering conventions.
Configuration Parameters
Two key configuration parameters control default ordering:
junit.jupiter.testmethod.order.default: Sets default method orderer for all test classes
junit.jupiter.testclass.order.default: Sets default class orderer for all test classes
Configuration Methods
These parameters can be set through:
junit-platform.properties file in test resources
- JVM system properties (
-D arguments)
- Maven Surefire Plugin configuration
- Programmatic configuration
Example
Setting global ordering properties
src/main/resources/src/test/resources/junit-platform.propertiesjunit.jupiter.testmethod.order.default = org.junit.jupiter.api.MethodOrderer$MethodName
junit.jupiter.testclass.order.default = org.junit.jupiter.api.ClassOrderer$OrderAnnotation
Above properties set default ordering strategies for our tests. First one sets default method ordering to alphabetical by method name for all test classes. Second one sets default class ordering to use @Order annotations for all test classes.
Test classes
package com.logicbig.example;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
@Order(3)
class AlphaTest {
@Test
void testB() {
System.out.println("AlphaTest.testB");
}
@Test
void testA() {
System.out.println("AlphaTest.testA");
}
}
package com.logicbig.example;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
@Order(1)
class BetaTest {
@Test
void testB() {
System.out.println("BetaTest.testB");
}
@Test
void testA() {
System.out.println("BetaTest.testA");
}
}
Following class overrides global method ordering by using @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
package com.logicbig.example;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@Order(4)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class GammaTest {
@Test
@Order(2)
void testB() {
System.out.println("Gamma.testB");
}
@Test
@Order(3)
void testA() {
System.out.println("Gamma.testA");
}
}
Following test class, uses @TestClassOrder(ClassOrderer.DisplayName.class) to specify it's on nested class ordering strategy.
package com.logicbig.example;
import org.junit.jupiter.api.*;
@Order(2)
@TestClassOrder(ClassOrderer.DisplayName.class)
class DeltaTest {
@Nested
@DisplayName("2 - Second Nested Class")
class SecondNested {
@Test
void testB() {
System.out.println("SecondNested.testB");
}
@Test
void testA() {
System.out.println("SecondNested.testA");
}
}
@Nested
@DisplayName("1 - First Nested Class")
class FirstNested {
@Test
void testB() {
System.out.println("FirstNested.testB");
}
@Test
void testA() {
System.out.println("FirstNested.testA");
}
}
}
Running all tests
$ mvn clean test [INFO] Scanning for projects... [INFO] [INFO] --< com.logicbig.example:junit-5-default-order-configuration-parameters >-- [INFO] Building junit-5-default-order-configuration-parameters 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- clean:3.2.0:clean (default-clean) @ junit-5-default-order-configuration-parameters --- [INFO] Deleting D:\example-projects\junit-5\junit-5-ordering\junit-5-default-order-configuration-parameters\target [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-default-order-configuration-parameters --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-ordering\junit-5-default-order-configuration-parameters\src\main\resources [INFO] [INFO] --- compiler:3.11.0:compile (default-compile) @ junit-5-default-order-configuration-parameters --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-default-order-configuration-parameters --- [WARNING] Using platform encoding (Cp1252 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.11.0:testCompile (default-testCompile) @ junit-5-default-order-configuration-parameters --- [INFO] Changes detected - recompiling the module! :source [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent! [INFO] Compiling 4 source files with javac [debug target 17] to target\test-classes [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-default-order-configuration-parameters --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- BetaTest.testA BetaTest.testB [INFO] +--com.logicbig.example.BetaTest - 0.071 ss [INFO] | +-- [OK] testA - 0.039 ss [INFO] | '-- [OK] testB - 0.007 ss FirstNested.testA FirstNested.testB SecondNested.testA SecondNested.testB [INFO] +--.--DeltaTest 1 - First Nested Class - 0.008 ss [INFO] | | +-- [OK] testA - 0.003 ss [INFO] | | '-- [OK] testB - 0.002 ss [INFO] +-----DeltaTest 2 - Second Nested Class - 0.008 ss [INFO] | +-- [OK] testA - 0.001 ss [INFO] | '-- [OK] testB - 0 ss AlphaTest.testA AlphaTest.testB [INFO] +--com.logicbig.example.AlphaTest - 0.002 ss [INFO] | +-- [OK] testA - 0.001 ss [INFO] | '-- [OK] testB - 0 ss Gamma.testB Gamma.testA [INFO] +--com.logicbig.example.GammaTest - 0.004 ss [INFO] | +-- [OK] testB - 0.001 ss [INFO] | '-- [OK] testA - 0.001 ss [INFO] [INFO] Results: [INFO] [INFO] Tests run: 10, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.162 s [INFO] Finished at: 2025-12-22T15:24:48+08:00 [INFO] ------------------------------------------------------------------------
As seen above, all top-level test classes are executed according to the global default ClassOrderer$OrderAnnotation, which determines class execution order based on their @Order values. Within AlphaTest and BetaTest, test methods are executed using the global default MethodOrderer$MethodName, resulting in alphabetical method execution. GammaTest explicitly overrides the global method ordering by declaring @TestMethodOrder(MethodOrderer$OrderAnnotation), causing its test methods to run according to their @Order annotations instead. DeltaTest has nested classes, the tests in that class are ordered by their parent’s @TestClassOrder(ClassOrderer$DisplayName), while method execution inside each nested class still follows the global default. Together, this demonstrates that JUnit evaluates ordering at each container level separately and always applies the closest applicable configuration.
Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.8.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 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
|