How to Run JUnit Tests in Order

In this tutorial, I am going to share with you how to make your JUnit tests run in order. And if you are interested to learn more about testing your RESTful Web Services with JUnit and Rest Assured check this list of tutorials: Building and Testing RESTful Web Services

By default, test classes and methods will be ordered using an algorithm that is deterministic but intentionally non obvious. 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.

In this tutorial you will learn how to apply order on your unit test methods using @TestMethodOrder annotation.

Junit 5 @TestMethodOrder Annotation

The @TestMethodOrder annotation allows us to use the MethodOrderer interface and specify which order we would like to apply to JUnit methods.

Using MethodOrderer, we can order our test methods by

  • Name,
  • Order Index,
  • Random or
  • order them by a Display Name.

Let’s see how to run test methods in random order first.

Random Order

To make your JUnit 5 test methods execute in random order, annotate test class with @TestMethodOrder(MethodOrderer.Random.class).  

Below is a complete code example that will execute test methods in random order. Notice that the class is annotated with @TestMethodOrder annotation and it specifies the order using MethodOrderer interface

import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

@TestMethodOrder(MethodOrderer.Random.class)
public class MethodOrderedRandomlyTest {

    @Test
    void testA() {
        System.out.println("Running test A");
    }

    @Test
    void testB() {
        System.out.println("Running test B");
    }

    @Test
    void testC() {
        System.out.println("Running test C");
    }

    @Test
    void testD() {
        System.out.println("Running test D");
    }
}

Order by Method Name

To order your JUnit 5 test methods by method name, annotate test class with @TestMethodOrder(MethodOrderer.MethodName.class).  

The code example shows how to use the MethodOrderer interface to configure the @TestMethodOrder annotation to order test methods by name.

package com.appsdeveloperblog;

import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

@TestMethodOrder(MethodOrderer.MethodName.class)
public class MethodsOrderedByNameTest {

    @Test
    void testD() {
        System.out.println("Running test D");
    }

    @Test
    void testA() {
        System.out.println("Running test A");
    }

    @Test
    void testC() {
        System.out.println("Running test C");
    }

    @Test
    void testB() {
        System.out.println("Running test B");
    }




}

Custom Order

To tell JUnit 5 which test method we want to execute first, and which test method we want to execute second, we need to do the following:

  1. Annotate test class with @TestInstance(TestInstance.Lifecycle.PER_CLASS). When using this mode, a new test instance will be created once per test class.
  2. Annotate each test method with @Order(x) annotation. Where x is an order index.

Below is a complete code example that orders JUnit 5 test methods in specific order.

package com.appsdeveloperblog;

import org.junit.jupiter.api.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MethodsOrderedByOrderIndexTest {


    @Order(1)
    @Test
    void testD() {
        System.out.println("Running test D");
    }

    @Order(2)
    @Test
    void testA() {
        System.out.println("Running test A");
    }

    @Order(3)
    @Test
    void testC() {
        System.out.println("Running test C");
    }

    @Order(4)
    @Test
    void testB() {
        System.out.println("Running test B");
    }

}

JUnit 4  Method Sorters

To run Unit tests in a specific order we can use the @FixMethodOrder annotation and the MethodSorters class which has three options: 

  • MethodSorters.DEFAULT – Sorts the test methods in a deterministic, but not predictable, order
  • MethodSorters.NAME_ASCENDING – Sorts the test methods by the method name, in lexicographic order, with {@link Method#toString()} used as a tiebreaker
  • MethodSorters.JVM – Leaves the test methods in the order returned by the JVM. Note that the order from the JVM may vary from run to run.

Run JUnit 4 Tests in Order:  From Top to Bottom

For me to be able to run my JUnit tests from top to bottom in the order I will use the MethodSorters.NAME_ASCENDING option. And to guarantee my methods are sorted in an ascending order I will give them very simple names: a(), b(), c(), d() and etc. This will make your JUnit test to always run in an order you want. Have a look at the below example. 

package com.appsdeveloperblog.app.ws.test.mobileappwstest;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class UsersWebServiceEndpointTest {
 
    @Before
    public void setup() {
        System.out.println("This method will run before each test method");
    }

    /*
     * You can add method description here
     */
    @Test 
    public void a() 
    {  
        System.out.println("This method will run first");
    }
    
    /*
     * You can add method description here
     */ 
    @Test 
    public void b() {
       System.out.println("This method will run second"); 
    }
    
    /*
     * You can add method description here
     */ 
    @Test 
    public void c() {
       System.out.println("This method will run third"); 
    }

    /*
     * You can add method description here
     */ 
    @Test 
    public void d() {
       System.out.println("This method will run last"); 
    }

}

I hope this tutorial was helpful to you. Have a look at the below list of video courses that teach how to test your code and hopefully one of them might be of a great value to you.

Leave a Reply

Your email address will not be published. Required fields are marked *