2.9. Test Execution Order

By default, test methods will be ordered using an algorithm that is deterministic but intentionally nonobvious. This ensures that subsequent runs of a test suite execute test methods in the same order, thereby allowing for repeatable builds.

See Test Classes and Methods for a definition of test method.

Although true unit tests typically should not rely on the order in which they are executed, there are times when it is necessary to enforce a specific test method execution order — for example, when writing integration tests or functional tests where the sequence of the tests is important, especially in conjunction with @TestInstance(Lifecycle.PER_CLASS).

To control the order in which test methods are executed, annotate your test class or test interface with [@TestMethodOrder](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/TestMethodOrder.html) and specify the desired [MethodOrderer](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/MethodOrderer.html) implementation. You can implement your own custom MethodOrderer or use one of the following built-in MethodOrderer implementations.

  • [DisplayName](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/MethodOrderer.DisplayName.html): sorts test methods alphanumerically based on their display names (see display name generation precedence rules)

  • [MethodName](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/MethodOrderer.MethodName.html): sorts test methods alphanumerically based on their method name and formal parameter lists.

  • [OrderAnnotation](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/MethodOrderer.OrderAnnotation.html): sorts test methods numerically based on values specified via the [@Order](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/Order.html) annotation.

  • [Random](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/MethodOrderer.Random.html): orders test methods pseudo-randomly and supports configuration of a custom seed.

  • [Alphanumeric](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/MethodOrderer.Alphanumeric.html): sorts test methods alphanumerically based on their names and formal parameter lists. Deprecated in favor of [MethodName](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/MethodOrderer.MethodName.html). Will be removed in 6.0

See also: Wrapping Behavior of Callbacks

The following example demonstrates how to guarantee that test methods are executed in the order specified via the @Order annotation.

  1. import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
  2. import org.junit.jupiter.api.Order;
  3. import org.junit.jupiter.api.Test;
  4. import org.junit.jupiter.api.TestMethodOrder;
  5. @TestMethodOrder(OrderAnnotation.class)
  6. class OrderedTestsDemo {
  7. @Test
  8. @Order(1)
  9. void nullValues() {
  10. // perform assertions against null values
  11. }
  12. @Test
  13. @Order(2)
  14. void emptyValues() {
  15. // perform assertions against empty values
  16. }
  17. @Test
  18. @Order(3)
  19. void validValues() {
  20. // perform assertions against valid values
  21. }
  22. }

2.9.1. Setting the Default Method Orderer

You can use the junit.jupiter.testmethod.order.default configuration parameter to specify the fully qualified class name of the [MethodOrderer](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/MethodOrderer.html) you would like to use by default. Just like for the orderer configured via the [@TestMethodOrder](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/TestMethodOrder.html) annotation, the supplied class has to implement the MethodOrderer interface. The default orderer will be used for all tests unless the @TestMethodOrder annotation is present on an enclosing test class or test interface.

For example, to use the [OrderAnnotation](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/MethodOrderer.OrderAnnotation.html) method orderer by default, you should set the configuration parameter to the corresponding fully qualified class name (e.g., in src/test/resources/junit-platform.properties):

  1. junit.jupiter.testmethod.order.default = \
  2. org.junit.jupiter.api.MethodOrderer$OrderAnnotation

Similarly, you can specify the fully qualified name of any custom class that implements MethodOrderer.