The MethodOrderer.MethodName strategy orders test methods alphabetically by their method names using natural string comparison. This provides a simple, predictable ordering based on method naming conventions.
When to Use MethodName Ordering
This ordering is useful when you want consistent execution order without annotating every method, or when following a naming convention that reflects test sequence (e.g., test01_Setup, test02_Execution, test03_Verification).
Sorting Details
Methods are sorted lexicographically using String::compareTo. Case sensitivity follows standard Java string comparison rules, with uppercase letters sorting before lowercase letters.
Examples
package com.logicbig.example;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static org.junit.jupiter.api.Assertions.*;
@TestMethodOrder(MethodOrderer.MethodName.class)
public class MethodOrdererMethodNameTest {
@Test
void test_BetaExecution() {
System.out.println("test_BetaExecution executed (starts with 'B')");
assertTrue(true);
}
@Test
void test_AlphaSetup() {
System.out.println("test_AlphaSetup executed (starts with 'A' - executes first)");
assertTrue(true);
}
@Test
void test_GammaVerification() {
System.out.println("test_GammaVerification executed (starts with 'G')");
assertTrue(true);
}
@Test
void test_DeltaCleanup() {
System.out.println("test_DeltaCleanup executed (starts with 'D')");
assertTrue(true);
}
@Test
void test_epsilonLowerCase() {
// Lowercase 'e' comes after uppercase letters
System.out.println("test_epsilonLowerCase executed (lowercase 'e' - executes last)");
assertTrue(true);
}
}
Output$ mvn test -Dtest=MethodOrdererMethodNameTest [INFO] Scanning for projects... [INFO] [INFO] ------< com.logicbig.example:junit-5-method-orderer-method-name >------- [INFO] Building junit-5-method-orderer-method-name 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-method-orderer-method-name --- [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-method-name\src\main\resources [INFO] [INFO] --- compiler:3.11.0:compile (default-compile) @ junit-5-method-orderer-method-name --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-method-orderer-method-name --- [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-method-name\src\test\resources [INFO] [INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ junit-5-method-orderer-method-name --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-method-orderer-method-name --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- test_AlphaSetup executed (starts with 'A' - executes first) test_BetaExecution executed (starts with 'B') test_DeltaCleanup executed (starts with 'D') test_GammaVerification executed (starts with 'G') test_epsilonLowerCase executed (lowercase 'e' - executes last) [INFO] +--com.logicbig.example.MethodOrdererMethodNameTest - 0.144 ss [INFO] | +-- [OK] test_AlphaSetup - 0.080 ss [INFO] | +-- [OK] test_BetaExecution - 0.013 ss [INFO] | +-- [OK] test_DeltaCleanup - 0.003 ss [INFO] | +-- [OK] test_GammaVerification - 0.002 ss [INFO] | '-- [OK] test_epsilonLowerCase - 0.002 ss [INFO] [INFO] Results: [INFO] [INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.312 s [INFO] Finished at: 2025-12-18T12:09:41+08:00 [INFO] ------------------------------------------------------------------------
package com.logicbig.example;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static org.junit.jupiter.api.Assertions.assertTrue;
@TestMethodOrder(MethodOrderer.MethodName.class)
class SequentialProcessTest {
@Test
void step3_Verification() {
System.out.println("step3_Verification executed");
assertTrue(true);
}
@Test
void step1_Initialization() {
System.out.println("step1_Initialization executed (first due to '1')");
assertTrue(true);
}
@Test
void step2_Processing() {
System.out.println("step2_Processing executed");
assertTrue(true);
}
}
Output$ mvn test -Dtest=SequentialProcessTest [INFO] Scanning for projects... [INFO] [INFO] ------< com.logicbig.example:junit-5-method-orderer-method-name >------- [INFO] Building junit-5-method-orderer-method-name 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-method-orderer-method-name --- [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-method-name\src\main\resources [INFO] [INFO] --- compiler:3.11.0:compile (default-compile) @ junit-5-method-orderer-method-name --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-method-orderer-method-name --- [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-method-name\src\test\resources [INFO] [INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ junit-5-method-orderer-method-name --- [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 2 source files with javac [debug target 17] to target\test-classes [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-method-orderer-method-name --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- step1_Initialization executed (first due to '1') step2_Processing executed step3_Verification executed [INFO] +--com.logicbig.example.SequentialProcessTest - 0.113 ss [INFO] | +-- [OK] step1_Initialization - 0.060 ss [INFO] | +-- [OK] step2_Processing - 0.015 ss [INFO] | '-- [OK] step3_Verification - 0.003 ss [INFO] [INFO] Results: [INFO] [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.851 s [INFO] Finished at: 2025-12-18T12:06:11+08:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - 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
|