Introduction
In this chapter, we will explore how to control the execution order of tests in JUnit. By default, JUnit executes tests in an unpredictable order, which is often desirable to ensure that tests are independent of each other. However, there are scenarios where controlling the execution order is useful, such as when tests depend on a specific sequence or when debugging complex test failures.
What is Test Execution Order?
Test execution order refers to the sequence in which test methods are executed within a test class. Controlling this order can help ensure that dependent tests run in the required sequence and can aid in debugging by consistently reproducing test failures.
Overview of @TestMethodOrder and @Order Annotations
JUnit 5 provides the @TestMethodOrder and @Order annotations to control the order of test method execution.
@TestMethodOrder
The @TestMethodOrder annotation is used at the class level to specify the ordering strategy for test methods within that class. JUnit provides several built-in ordering strategies:
- MethodOrderer.Alphanumeric: Orders test methods alphanumerically by their names.
- MethodOrderer.OrderAnnotation: Orders test methods based on the- @Orderannotation.
- MethodOrderer.Random: Orders test methods randomly.
- MethodOrderer.DisplayName: Orders test methods by their display names.
@Order
The @Order annotation is used to specify the order of individual test methods when MethodOrderer.OrderAnnotation is used. The lower the value, the earlier the method is executed.
Example: Controlling Test Execution Order
Let’s create examples to demonstrate how to control the execution order of tests using @TestMethodOrder and @Order.
Step 1: Create a Class Under Test
Calculator Class
The Calculator class will have basic arithmetic operations.
public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
    public int subtract(int a, int b) {
        return a - b;
    }
    public int multiply(int a, int b) {
        return a * b;
    }
    public int divide(int a, int b) {
        if (b == 0) {
            throw new IllegalArgumentException("Division by zero");
        }
        return a / b;
    }
}
Step 2: Create a Test Class with a Controlled Execution Order
CalculatorTest
The CalculatorTest class will contain tests for the Calculator class with controlled execution order.
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.MethodOrderer;
import static org.junit.jupiter.api.Assertions.*;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class CalculatorTest {
    private Calculator calculator = new Calculator();
    @Test
    @Order(1)
    void testAdd() {
        assertEquals(5, calculator.add(2, 3), "2 + 3 should equal 5");
    }
    @Test
    @Order(2)
    void testSubtract() {
        assertEquals(2, calculator.subtract(5, 3), "5 - 3 should equal 2");
    }
    @Test
    @Order(3)
    void testMultiply() {
        assertEquals(6, calculator.multiply(2, 3), "2 * 3 should equal 6");
    }
    @Test
    @Order(4)
    void testDivide() {
        assertEquals(2, calculator.divide(6, 3), "6 / 3 should equal 2");
    }
    @Test
    @Order(5)
    void testDivideByZero() {
        Exception exception = assertThrows(IllegalArgumentException.class, () -> {
            calculator.divide(1, 0);
        });
        assertEquals("Division by zero", exception.getMessage());
    }
}
Important Points
- @TestMethodOrder: Use this annotation at the class level to specify the ordering strategy for test methods. Available strategies include Alphanumeric,OrderAnnotation,Random, andDisplayName.
- @Order: Use this annotation to specify the execution order of individual test methods when using MethodOrderer.OrderAnnotation. The lower the value, the earlier the method is executed.
Running the Tests
To run the tests, simply run the CalculatorTest class as a JUnit test. This will execute all test methods in the specified order.
Using IntelliJ IDEA
- Run Tests: Click the green run icon next to the CalculatorTestclass and selectRun.
- View Results: The results will be displayed in the Run window, showing the tests executed in the specified order.

Using Eclipse
- Run Tests: Right-click on the CalculatorTestfile and selectRun As>JUnit Test.
- View Results: The results will be displayed in the JUnit view, showing the tests executed in the specified order.
Using VS Code
- Run Tests: Open the CalculatorTestfile and click theRunicon above the class declaration.
- View Results: The results will be displayed in the Test Explorer, showing the tests executed in the specified order.
Conclusion
JUnit provides a convenient way to control the execution order of tests using the @TestMethodOrder and @Order annotations. Controlling test execution order can be useful for ensuring dependent tests run in sequence, for debugging, and for consistently reproducing test failures. By specifying the execution order, you can create more predictable and manageable test suites.