Test Overview
This test demonstrates how to use the @Order annotation in JUnit 5 to control the order in which test methods run. It verifies that tests execute in the specified sequence.
This test demonstrates how to use the @Order annotation in JUnit 5 to control the order in which test methods run. It verifies that tests execute in the specified sequence.
import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class OrderedTests { private static StringBuilder executionOrder = new StringBuilder(); @Test @Order(2) void secondTest() { executionOrder.append("B"); assertTrue(true); } @Test @Order(1) void firstTest() { executionOrder.append("A"); assertTrue(true); } @Test @Order(3) void thirdTest() { executionOrder.append("C"); assertEquals("ABC", executionOrder.toString()); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | JUnit test runner starts and reads @TestMethodOrder annotation | JUnit recognizes tests will run in @Order sequence | - | PASS |
| 2 | Runs firstTest() annotated with @Order(1) | executionOrder is empty | assertTrue(true) always passes | PASS |
| 3 | Runs secondTest() annotated with @Order(2) | executionOrder contains "A" from firstTest | assertTrue(true) always passes | PASS |
| 4 | Runs thirdTest() annotated with @Order(3) | executionOrder contains "AB" from previous tests | assertEquals("ABC", executionOrder.toString()) verifies order | PASS |