JUnit 5 – Test Execution Order
Last Updated :
28 Apr, 2025
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.
Similar Reads
JUnit 5 â Conditional Test Execution JUnit is one of the popular unit testing frameworks for Java projects. JUnit 5 is the latest version provided by the JUnit team launched with the aim of supporting the latest Java features and also providing notable advanced features to its predecessor JUnit 4. In this article, we will discuss one o
8 min read
JUnit 5 â Execute Test in Eclipse JUnit is one of the widely used unit testing frameworks for Java-based Applications. We have different versions and upgrades published by JUnit 5, the latest version available in JUnit is JUnit 5. JUnit 5 is the upgraded version of JUnit 4, which is launched to support various latest features of Jav
10 min read
JUnit â Ordered Tests JUnit is a popular framework in the Java ecosystem designed for creating and executing automated tests. Although the framework encourages writing independent tests that can run in any order, there are scenarios where tests need to be executed in a specific order. This article explores how to order t
7 min read
JUnit â Executing Tests with Maven Build JUnit is a widely-used testing framework for Java that allows developers to create and execute unit tests. When combined with Maven, the build automation tool, it becomes easy to automate the execution of these tests as part of the build lifecycle. In this article, we will explore how JUnit works wi
6 min read
How to Control Test Execution Order in TestNG? Controlling the order in which tests run is crucial, especially in larger software projects. By controlling the test execution order, you ensure that tests run logically. This helps in identifying real issues in the code, rather than false negatives caused by running tests out of sequence. It also m
3 min read