Updated February 17, 2023
Introduction to JUnit Test Order
The JUnit test order method is invoked to return the reflection API. While using JVM, the order is not specific since java is not specifying any specific order, so we can say that JDK 7 returns any order. However, it is beneficial and essential while testing the test cases. In addition, alphanumeric ordering is used to sort the test method alphanumerically by using their names.
Overview
- From version 4.11, JUnit is used by default as deterministic order, but we can say that it is not predictable. Therefore, we need to use different annotations to change the order execution while creating test case code.
- The JUnit 5 allows users to override the order and run the test in order of determination. We can specify the numeric as well as alphanumeric order.
- We can run the JUnit test using numeric order by specifying the @TestMethodOrder annotation on the class level and need to select each method number using the @Order annotation.
JUnit Test Order Classes
Below are the classes used while testing the test cases by order as follows:
1. MethodName.class
This class is used to sort the test alphanumerically based on the test names. The below example shows sorting the tests as per method names. In the below example, we have defined MethodName.class by using test method order. We have created the MethodOrderedTest class to use JUnit test order classes.
Code:
@TestMethodOrder (MethodOrderer.MethodName.class)
public class MethodOrderedTest
{
@Test
void ordE () {
assertTrue (true);
}
@Test
Void ordA () {
assertTrue (true);
}
@Test
void OrdD () {
assertTrue (true);
}
@Test
void OrdC () {
assertTrue (true);
}
@Test
void ordB () {
assertTrue (true);
}
}
Output:
2. DisplayName.class
This class is used to sort the test based on the display names. The below example shows sorting the tests as per display names as follows. In the below example, we have defined DisplayName.class by using test method order. In addition, we have created the MethodDisplayTest class to use classes.
Code:
@TestMethodOrder (MethodOrderer.DisplayName.class)
public class MethodDisplayTest
{
@DisplayName ("2")
@Test
void ordE ()
{
assertTrue (true);
}
@DisplayName ("4")
@Test
void ordA () {
assertTrue (true);
}
@DisplayName ("3")
@Test
void OrdD ()
{
assertTrue (true);
}
@DisplayName ("5")
@Test
void OrdC ()
{
assertTrue (true);
}
@DisplayName ("1")
@Test
void ordB ()
{
assertTrue (true);
}
}
Output:
3. OrderAnnotation.class
This class is used to sort the test based on the order annotation. The below example shows sorting the tests as per annotation of the order. In the below example, we defined OrderAnnotation.class using the test method order. We have created the OrderAnnotationTest class to use JUnit order annotation classes.
Code:
@TestMethodOrder (MethodOrderer.OrderAnnotation.class)
public class OrderAnnotationTest
{
@Order (2)
@Test
void ordE () {
assertTrue (true);
}
@Order (4)
@Test
void ordA () {
assertTrue (true);
}
@Order (3)
@Test
void OrdD ()
{
assertTrue (true);
}
@Order (5)
@Test
void OrdC ()
{
assertTrue (true);
}
@Order (1)
@Test
void ordB ()
{
assertTrue (true);
}
}
Output:
4. Random.class
This class is used to sort the test based on the random sequence. The below example shows sorting the tests per random order as follows. In the example below, we have defined the Random.class using test method order. In addition, we have created the RandomTest class to use classes.
Code:
@TestMethodOrder (MethodOrderer.Random.class)
public class RandomTest
{
@Test
void ordE () {
assertTrue (true);
}
@Test
void ordA () {
assertTrue (true);
}
@Test
void OrdD () {
assertTrue (true);
}
@Test
void OrdC () {
assertTrue (true);
}
@Test
void ordB () {
assertTrue (true);
}
}
Output:
Examples of JUnit Test Order
Different examples are mentioned below:
Example #1
The below example shows the JUnit test methods of MethodSorters.DEFAULT. This enum value is used to sort the execution as per specific order. It is not predictable in what order the test case will run. At the time of executing the order with MethodSorters DEFAULT, the order will not change its order; it remains the same.
Code:
@FixMethodOrder (MethodSorters.DEFAULT)
public class JUnitTest1 {
@Test
public void case3 () {
System.out.println ("Third test case");
}
@Test
public void case1 () {
System.out.println ("First test case");
}
@Test
public void case2() {
System.out.println ("Second test case");
}
@Test
public void case4 () {
System.out.println ("Fourth test case");
}
@Test
public void case5 () {
System.out.println ("Fifth test case");
}
}
Output:
Example #2
The below example shows the JUnit method will order by using alphanumeric.
We are using the below annotation to order alphanumeric ordering values as follows.
- @TestMethodOrder
- @Order
The class we are using belongs to the interface of the method orderer. The alphanumeric ordering is used to sort the test method alphanumerically by using their names.
Code:
@FixMethodOrder (Alphanumeric.class)
public class Alphanumeric {
@Test
public void case3 () {
System.out.println ("Third test case");
}
@Test
public void case1 () {
System.out.println ("First test case");
}
@Test
public void case2() {
System.out.println ("Second test case");
}
@Test
public void case4 () {
System.out.println ("Fourth test case");
}
@Test
public void case5 () {
System.out.println ("Fifth test case");
}
}
Output:
Example #3
The below example shows creating the order annotation as follows. JUnit version 5 supports order annotation class. This method also plays a vital role in displaying the records per specified order.
Code:
@FixMethodOrder (OrderAnnotation.class)
public class Annotation {
@Test
@Order (3)
public void case3 () {
System.out.println ("Third test case");
}
@Test
@Order (4)
public void case1 () {
System.out.println ("First test case");
}
@Test
@Order (1)
public void case2() {
System.out.println ("Second test case");
}
@Test
@Order (5)
public void case4 () {
System.out.println ("Fourth test case");
}
@Test
@Order (2)
public void case5 () {
System.out.println ("Fifth test case");
}
}
Output:
Conclusion
We can run the JUnit test using numeric order by specifying the @TestMethodOrder annotation on the class level. However, JUnit 5 allows users to override the order and run the test in order of determination. Therefore, we can specify the numeric as well as alphanumeric order.
Recommended Articles
This is a guide to JUnit Test Order. Here we discuss the introduction, JUnit test order classes, and examples for better understanding. You may also have a look at the following articles to learn more –