Contents

Order tests in JUnit5 and JUnit4

Writen by: David Vlijmincx

Introduction

Good unit tests should not rely on the order that they are run. But sometimes, there a test cases that you need to run in a specific order, for example, integration tests.

JUnit5 Built-in orderers

Junit offers five built-in ways to control the order in which the tests are executed:

  • OrderAnnotation: We can use annotations to execute tests in a specific order.
  • DisplayName: Sorts test by their display name.
  • MethodName: Orders the test by their method name.
  • Alphanumeric: This class is deprecated and will be removed with JUnit6. It works the same as MethodName.
  • Random: This causes your tests to be run in a different order each time.

Using the @Order annotation

With @TestMethodOrder(OrderAnnotation.class) above your test class, you can use the @Order annotation to specify the order the tests are executed. The following example will first run the test with @Order(1), followed by the test annotated with @Order(2).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
@TestMethodOrder(OrderAnnotation.class)
public class TestOrder {
    
    @Test
    @Order(1)
    void firstTest() {
        System.out.println("1");
    }

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

    @Test
    @Order(3)
    void thirdTest() {
        System.out.println("3");
    }
}

Run tests in order of their method name

When you annotate your test class with @TestMethodOrder(MethodName.class), the test will be executed according to its method name and parameter list. In the following example the execution order would be: a(), a(int i), b().

You could also use @TestMethodOrder(Alphanumeric.class), but I would not recommend it as it will be removed with JUnit6.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
@TestMethodOrder(MethodName.class)
class TestOrderByMethodName {

    @Test
    void a() {
        System.out.println("a");
    }

    @ParameterizedTest
    @ValueSource(ints = {1})
    void a(int i) {
        System.out.println("a: " + i);
    }

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

}

Run test in order of their display name

You can also choose to execute tests in the order of their display name, as in the following example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
@TestMethodOrder(org.junit.jupiter.api.MethodOrderer.DisplayName.class)
class TestOrderByDisplayName {

    @Test
    @DisplayName("a")
    void a() {
        System.out.println("a");
    }

    @Test
    @DisplayName("b")
    void b() {
        System.out.println("b");
    }

    @Test
    @DisplayName("b")
    void c() {
        System.out.println("c");
    }
}

Ordering tests in JUnit4

Setting the execution order of tests in JUnit4 is different from JUnit5. The following example orders the test execution by method name.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
@OrderWith(Alphanumeric.class)
public class TestOrderWithJunit4 {

    @Test
    public void a(){
        System.out.println("a");
    }

    @Test
    public void b(){
        System.out.println("b");
    }

    @Test
    public void c(){
        System.out.println("c");
    }

}

Conclusion

This article looked at different ways to run your tests in a specific order using JUnit. If you want to know more about the execution order or JUnit5, please see the official docs.

Further reading

More about testing in Java:

 




Questions, comments, concerns?

Have a question or comment about the content? Feel free to reach out!