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
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING