Open In App

JUnit 5 – Test Execution Order

Last Updated : 03 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JUnit 5, the Test execution order is not in the order by default. Every Test is randomly executed one after the other. There is no compulsion that the test which is on top will be executed first and the test which is in bottom will be executed last. So, the order of execution of test cases is unpredictable. JUnit 5 is now giving the programmer to prioritize the tests according to their order.

Prerequisites

  • Knowledge of Java Programming Language.
  • Hands-on experience with Eclipse IDE is a plus.

Basically, in JUnit 5 we have 3 ways to control the order of test execution:

  • By Annotation Order
  • Alphanumeric Order
  • Random Order

Setting up Junit 5 Jar Files

We’ll see one after the other how are the above three orders implemented and executed. We will write the test cases in a normal Java project. Firstly, to make sure our JUnit works in our program download the Junit5 jar files from online and import it in your IDE. To import the jar files into your Java project:

Right-click on your Java > Select the option “Buildpath” > Select “Add libraries”. Then import the jar from your local computer into the libraries.

Example – Main Class:

Now we will go through an example where we will create a class and test the program through different test execution orders.

Java




import java.io.*;
  
public class program {
  
        public  int findSum(int a,int b)
        {
            return a+b;
        }
      
  
        public int findDiff(int a,int b)
        {
            return a-b;
        }
          
        public int findMult(int a,int b)
        {
            return a*b;
        }
          
  
}


In the above program, we have created a class with three methods which does three arithmetic operations. We will be testing our program through different Test Execution Order.

  • findSum() calculates the summation of two integers.
  • findDiff() calculates the difference of two integers.
  • findMult() calculates the product of two integers.

Note: To implement the test execution order we have to annotate the test class with @TestMethodOrder(). Inside this we have to mention which type of Test Execution Order we will be implementing in order for controlling the test cases.

1. Annotation Order

The test execution order here will depend on the order number we give inside the @order annotation for the method we want to test. So, according to the value which is inside the @order annotation the execution of test cases will be taken place.

Java




import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
  
  
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class TestCode {
  
    @Test
    @Order(2)
    public void testdiff()
    {
        program obj=new program();
        assertEquals(2,obj.findDiff(4,2));
        System.out.println("In testdiff");
    }
      
    @Test
    @Order(1)
    public void testmul()
    {
        program obj=new program();
        assertEquals(8,obj.findMult(4,2));
        System.out.println("In testmul");
    }
  
}


Output:

In testmul
In testdiff

Explanation:

  • So, if we notice here the testdiff method is before the testmul method according to the program.
  • But in the output testmul method is executed before the testdiff. This is because we annotated the method with value inside the @order annotation.
  • In this way by annotating our test methods with @order annotation we can control the test cases execution.

2. Alphanumeric Order

The test execution order here will be sorted in a lexicographic (alphanumerical) order. So, based on the method names the test cases will be executed. In order the test cases to execute in this way we must annotate the test class with @TestMethodOrder(MethodOrderer.Alphanumeric.class).

Java




import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
  
  
@TestMethodOrder(MethodOrderer.Alphanumeric.class)
public class TestCode {
  
    @Test
    public void testA()
    {
        program obj=new program();
        assertEquals(2,obj.findDiff(4,2));
        System.out.println("In testdiff");
    }
      
    @Test
    public void testB()
    {
        program obj=new program();
        assertEquals(8,obj.findMult(4,2));
        System.out.println("In testmul");
    }
  
}


Output:

In testdiff
In testmul

3.Random Order

The test execution order here is executed randomly. There will be no specific order that when a test case is executed. So here we us TestMethodOrder(MethodOrderer.Random.class) annotation to execute the test cases randomly when we run the test cases.

Java




import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.TestMethodOrder;
  
  
@TestMethodOrder(MethodOrderer.Random.class)
public class TestCode {
  
    @Test
    public void testA()
    {
        program obj=new program();
        assertEquals(2,obj.findDiff(4,2));
        System.out.println("In testdiff");
    }
      
    @Test
    public void testB()
    {
        program obj=new program();
        assertEquals(8,obj.findMult(4,2));
        System.out.println("In testmul");
    }
  
}


Output:

At Run 1:

In testdiff
In testmul

At Run 2:

In testmul
In testdiff

At Run 3:

In testdiff
In testmul

So, we can observe in the above output that we have run “my test” class three times. In each run it’s giving different Execution Order for the test cases.

Conclusion

In conclusion, the Junit 5 ensures the independence of testing the test cases more reliable. With these testing order It ensures that we can control the test cases according to our need or requirement. But as a developer it is important to note that relying more on the test execution order is not a good practice as it might lead to lack of robustness of the test suite.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads