0
0
JunitHow-ToBeginner ยท 4 min read

How to Use @Order Annotation in JUnit for Test Execution Order

In JUnit 5, use the @Order annotation on test methods or test classes to specify the order in which tests run. Combine it with @TestMethodOrder on the test class to activate ordering. Lower @Order values run first.
๐Ÿ“

Syntax

The @Order annotation takes an integer value that defines the execution priority of a test method or class. It must be used together with @TestMethodOrder on the test class to enable ordering.

  • @TestMethodOrder(MethodOrderer.OrderAnnotation.class): Activates ordering by @Order.
  • @Order(int value): Specifies the order; smaller numbers run first.
java
import org.junit.jupiter.api.*;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class OrderedTests {

    @Test
    @Order(1)
    void firstTest() {
        System.out.println("First test");
    }

    @Test
    @Order(2)
    void secondTest() {
        System.out.println("Second test");
    }
}
๐Ÿ’ป

Example

This example shows two test methods with @Order annotations. The test with @Order(1) runs before the one with @Order(2). The @TestMethodOrder annotation on the class enables this behavior.

java
import org.junit.jupiter.api.*;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class ExampleOrderTest {

    @Test
    @Order(2)
    void testSecond() {
        System.out.println("Running second test");
        Assertions.assertTrue(true);
    }

    @Test
    @Order(1)
    void testFirst() {
        System.out.println("Running first test");
        Assertions.assertTrue(true);
    }
}
Output
Running first test Running second test
โš ๏ธ

Common Pitfalls

Common mistakes when using @Order include:

  • Not adding @TestMethodOrder(MethodOrderer.OrderAnnotation.class) on the test class, so @Order is ignored.
  • Using duplicate order values, which can cause unpredictable execution order.
  • Assuming test order is guaranteed without @Order and @TestMethodOrder.
java
import org.junit.jupiter.api.*;

// Wrong: Missing @TestMethodOrder, so @Order is ignored
class WrongOrderTest {

    @Test
    @Order(1)
    void testOne() {
        System.out.println("Test one");
    }

    @Test
    @Order(2)
    void testTwo() {
        System.out.println("Test two");
    }
}

// Correct usage
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class CorrectOrderTest {

    @Test
    @Order(1)
    void testOne() {
        System.out.println("Test one");
    }

    @Test
    @Order(2)
    void testTwo() {
        System.out.println("Test two");
    }
}
๐Ÿ“Š

Quick Reference

AnnotationPurposeNotes
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)Enable ordering of test methods by @OrderPlace on test class
@Order(int value)Define execution order of test method or classLower value runs first
@TestMarks a method as a testRequired for test execution
Duplicate @Order valuesCan cause unpredictable orderAvoid duplicates
Missing @TestMethodOrder@Order is ignoredAlways add @TestMethodOrder
โœ…

Key Takeaways

Use @TestMethodOrder(MethodOrderer.OrderAnnotation.class) on the test class to enable @Order.
Annotate test methods with @Order(int) to specify execution order; smaller numbers run first.
Without @TestMethodOrder, @Order annotations have no effect.
Avoid duplicate @Order values to prevent unpredictable test execution order.
JUnit does not guarantee test order unless explicitly specified with @Order and @TestMethodOrder.