Test Method Order

Learn how to control the order of test method execution in JUnit 5.

In JUnit 5, we can control the execution order of test classes and methods.

Test method execution order

Test methods of a test class should be independent of each other, so these methods can be executed in any order. However, some test methods may have a specific execution order. We can use the org.junit.jupiter.api.TestMethodOrder annotation to control the execution order of test methods. The @TestMethodOrder uses an implementation of org.junit.jupiter.api.MethodOrderer to sort the test methods. The MethodOrderer provides four built-in implementations.

Get hands-on with 1400+ tech skills courses.