The MethodOrderer.Random strategy executes test methods in a random order for each test run. This helps detect test dependencies and ensures tests are truly independent by exposing hidden state sharing or order dependencies.
Purpose of Random Ordering
Random test ordering is a quality assurance technique that helps identify:
- Tests that depend on execution order
- Shared state between tests that isn't properly reset
- Hidden dependencies on previous test outcomes
- Non-deterministic test behavior
Random Seed Configuration
You can configure a specific random seed for reproducible test runs using configuration parameters. This allows you to reproduce specific random orderings when debugging order-dependent test failures. That means as long as you keep the same seed, JUnit will execute the tests in the same “random� order every time
Example
Setting a seed (optional)
We can set the seed globally in unit-platform.properties file:
src/main/resources/src/test/resources/junit-platform.propertiesjunit.jupiter.execution.order.random.seed = 49500
package com.logicbig.example;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.TestMethodOrder;
import static org.junit.jupiter.api.Assertions.*;
@TestMethodOrder(MethodOrderer.Random.class)
class IndependentTestsExample {
// These tests should pass regardless of execution order
// because they don't share state
@Test
void calculateSum() {
int result = 2 + 3;
assertEquals(5, result);
System.out.println("calculateSum executed");
}
@Test
void checkStringLength() {
String text = "Hello";
assertEquals(5, text.length());
System.out.println("checkStringLength executed");
}
@Test
void verifyBoolean() {
boolean flag = true;
assertTrue(flag);
System.out.println("verifyBoolean executed");
}
@Test
void testArrayEquality() {
int[] array1 = {1, 2, 3};
int[] array2 = {1, 2, 3};
assertArrayEquals(array1, array2);
System.out.println("testArrayEquality executed");
}
}
Output$ mvn test -Dtest=IndependentTestsExample [INFO] Scanning for projects... [INFO] [INFO] ---------< com.logicbig.example:junit-5-method-orderer-random >--------- [INFO] Building junit-5-method-orderer-random 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-method-orderer-random --- [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-method-orderer-random\src\main\resources [INFO] [INFO] --- compiler:3.11.0:compile (default-compile) @ junit-5-method-orderer-random --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-method-orderer-random --- [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-method-orderer-random\src\test\resources [INFO] [INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ junit-5-method-orderer-random --- [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 3 source files with javac [debug target 17] to target\test-classes [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-method-orderer-random --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- Test Delta executed. Current order: [Delta] Test Gamma executed. Current order: [Delta, Gamma] Test Epsilon executed. Current order: [Delta, Gamma, Epsilon] Final execution order: [Delta, Gamma, Epsilon] Test Beta executed. Current order: [Delta, Gamma, Epsilon, Beta] Test Alpha executed. Current order: [Delta, Gamma, Epsilon, Beta, Alpha] [INFO] +--com.logicbig.example.MethodOrdererRandomTest - 0.108 ss [INFO] | +-- [OK] testDelta - 0.042 ss [INFO] | +-- [OK] testGamma - 0.020 ss [INFO] | +-- [XX] testEpsilon - 0.014 ss [INFO] | +-- [OK] testBeta - 0.001 ss [INFO] | '-- [OK] testAlpha - 0.001 ss [INFO] [INFO] Results: [INFO] [ERROR] Failures: [ERROR] MethodOrdererRandomTest.testEpsilon:50 expected: <5> but was: <3> [INFO] [ERROR] Tests run: 5, Failures: 1, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.414 s [INFO] Finished at: 2025-12-18T13:51:17+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.5.0:test (default-test) on project junit-5-method-orderer-random: There are test failures. [ERROR] [ERROR] Please refer to D:\example-projects\junit-5\junit-5-ordering\junit-5-method-orderer-random\target\surefire-reports for the individual test results. [ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream. [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.4.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 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
|
|