Close

JUnit 5 - Explicit numerical ordering of test methods with @Order, MethodOrderer.OrderAnnotation Example

[Last Updated: Dec 22, 2025]

The MethodOrderer.OrderAnnotation strategy orders test methods based on numerical values specified with the @Order annotation. This provides explicit, fine-grained control over test execution sequence, making it ideal for tests with dependencies or specific flow requirements.

Order Values and Sorting

Tests are ordered in ascending order by their @Order annotation values. Lower numbers execute first. Methods without @Order annotations are considered to have the lowest priority and are executed before ordered methods.

Practical Applications

This ordering is particularly useful for integration tests that simulate user workflows, setup/teardown sequences, or performance tests requiring specific execution patterns. It provides the most control over test sequencing.

Example

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.OrderAnnotation.class)
public class MethodOrdererOrderAnnotationTest {

    private static StringBuilder executionLog = new StringBuilder();

    @Test
    @Order(3)
    void verifyResults() {
        executionLog.append("3-Verify ");
        System.out.println("Verify results executed (Order 3)");
        System.out.println("Current execution log: " + executionLog.toString());
        assertEquals("1-Setup 2-Execute 3-Verify ", executionLog.toString());
    }

    @Test
    @Order(1)
    void setupEnvironment() {
        executionLog.append("1-Setup ");
        System.out.println("Setup environment executed (Order 1 - first)");
        assertTrue(true);
    }

    @Test
    @Order(2)
    void executeProcess() {
        executionLog.append("2-Execute ");
        System.out.println("Execute process executed (Order 2)");
        assertTrue(true);
    }

    @Test
    @Order(4)
    void cleanupResources() {
        executionLog.append("4-Cleanup ");
        System.out.println("Cleanup resources executed (Order 4 - last)");
        System.out.println("Final execution log: " + executionLog.toString());
        assertEquals("1-Setup 2-Execute 3-Verify 4-Cleanup ", executionLog.toString());
    }

    @Test
    void testWithoutOrder() {
        // No @Order annotation - executes before ordered tests
        System.out.println("Test without @Order executed (before ordered tests)");
        assertTrue(true);
    }
}

Output

$ mvn test -Dtest=MethodOrdererOrderAnnotationTest
[INFO] Scanning for projects...
[INFO]
[INFO] ----< com.logicbig.example:junit-5-method-orderer-order-annotation >----
[INFO] Building junit-5-method-orderer-order-annotation 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-method-orderer-order-annotation ---
[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-order-annotation\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ junit-5-method-orderer-order-annotation ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-method-orderer-order-annotation ---
[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-order-annotation\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ junit-5-method-orderer-order-annotation ---
[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-order-annotation ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
Setup environment executed (Order 1 - first)
Execute process executed (Order 2)
Verify results executed (Order 3)
Current execution log: 1-Setup 2-Execute 3-Verify
Cleanup resources executed (Order 4 - last)
Final execution log: 1-Setup 2-Execute 3-Verify 4-Cleanup
Test without @Order executed (before ordered tests)
[INFO] +--com.logicbig.example.MethodOrdererOrderAnnotationTest - 0.108 ss
[INFO] | +-- [OK] setupEnvironment - 0.035 ss
[INFO] | +-- [OK] executeProcess - 0.022 ss
[INFO] | +-- [OK] verifyResults - 0.006 ss
[INFO] | +-- [OK] cleanupResources - 0.003 ss
[INFO] | '-- [OK] testWithoutOrder - 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: 4.162 s
[INFO] Finished at: 2025-12-18T13:32:42+08:00
[INFO] ------------------------------------------------------------------------
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.OrderAnnotation.class)
class UserRegistrationWorkflowTest {

    @Test
    @Order(10)
    void sendWelcomeEmail() {
        System.out.println("Send welcome email (Order 10)");
        assertTrue(true);
    }

    @Test
    @Order(5)
    void validateInputData() {
        System.out.println("Validate input data (Order 5)");
        assertTrue(true);
    }

    @Test
    @Order(1)
    void initializeRegistration() {
        System.out.println("Initialize registration (Order 1 - first)");
        assertTrue(true);
    }

    @Test
    @Order(20)
    void finalizeRegistration() {
        System.out.println("Finalize registration (Order 20 - last)");
        assertTrue(true);
    }

    @Test
    @Order(15)
    void createUserProfile() {
        System.out.println("Create user profile (Order 15)");
        assertTrue(true);
    }
}

Output

$ mvn  test -Dtest=UserRegistrationWorkflowTest
[INFO] Scanning for projects...
[INFO]
[INFO] ----< com.logicbig.example:junit-5-method-orderer-order-annotation >----
[INFO] Building junit-5-method-orderer-order-annotation 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-method-orderer-order-annotation ---
[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-order-annotation\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ junit-5-method-orderer-order-annotation ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-method-orderer-order-annotation ---
[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-order-annotation\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ junit-5-method-orderer-order-annotation ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- surefire:3.5.0:test (default-test) @ junit-5-method-orderer-order-annotation ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
Initialize registration (Order 1 - first)
Validate input data (Order 5)
Send welcome email (Order 10)
Create user profile (Order 15)
Finalize registration (Order 20 - last)
[INFO] +--com.logicbig.example.UserRegistrationWorkflowTest - 0.113 ss
[INFO] | +-- [OK] initializeRegistration - 0.049 ss
[INFO] | +-- [OK] validateInputData - 0.017 ss
[INFO] | +-- [OK] sendWelcomeEmail - 0.001 ss
[INFO] | +-- [OK] createUserProfile - 0.001 ss
[INFO] | '-- [OK] finalizeRegistration - 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.015 s
[INFO] Finished at: 2025-12-18T13:32:47+08:00
[INFO] ------------------------------------------------------------------------
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.OrderAnnotation.class)
class NegativeOrderValuesTest {

    @Test
    @Order(-5)
    void negativeOrderTest() {
        System.out.println("Negative order test (-5) executed");
        assertTrue(true);
    }

    @Test
    @Order(0)
    void zeroOrderTest() {
        System.out.println("Zero order test (0) executed");
        assertTrue(true);
    }

    @Test
    @Order(100)
    void largeOrderTest() {
        System.out.println("Large order test (100) executed");
        assertTrue(true);
    }
}

Output

$ mvn  test -Dtest=NegativeOrderValuesTest
[INFO] Scanning for projects...
[INFO]
[INFO] ----< com.logicbig.example:junit-5-method-orderer-order-annotation >----
[INFO] Building junit-5-method-orderer-order-annotation 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-method-orderer-order-annotation ---
[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-order-annotation\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ junit-5-method-orderer-order-annotation ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-method-orderer-order-annotation ---
[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-order-annotation\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ junit-5-method-orderer-order-annotation ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- surefire:3.5.0:test (default-test) @ junit-5-method-orderer-order-annotation ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
Negative order test (-5) executed
Zero order test (0) executed
Large order test (100) executed
[INFO] +--com.logicbig.example.NegativeOrderValuesTest - 0.088 ss
[INFO] | +-- [OK] negativeOrderTest - 0.044 ss
[INFO] | +-- [OK] zeroOrderTest - 0.009 ss
[INFO] | '-- [OK] largeOrderTest - 0.001 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.031 s
[INFO] Finished at: 2025-12-18T13:32:53+08:00
[INFO] ------------------------------------------------------------------------

Example Project

Dependencies and Technologies Used:

  • junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
     Version Compatibility: 5.4.0 - 6.0.1Version List
    ×

    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

JUnit 5 - MethodOrderer.OrderAnnotation Select All Download
  • junit-5-method-orderer-order-annotation
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • MethodOrdererOrderAnnotationTest.java

    See Also