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@Orderis ignored. - Using duplicate order values, which can cause unpredictable execution order.
- Assuming test order is guaranteed without
@Orderand@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
| Annotation | Purpose | Notes |
|---|---|---|
| @TestMethodOrder(MethodOrderer.OrderAnnotation.class) | Enable ordering of test methods by @Order | Place on test class |
| @Order(int value) | Define execution order of test method or class | Lower value runs first |
| @Test | Marks a method as a test | Required for test execution |
| Duplicate @Order values | Can cause unpredictable order | Avoid duplicates |
| Missing @TestMethodOrder | @Order is ignored | Always 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.