Introduction
Welcome readers! This blog will learn how to run JUnit tests in a determined order. You know, when running a whole test class, the execution order of test methods matters if the input of a test depends on the output of others. For example, when testing CRUD operations, testList() method would depend on testCreate() method – so when running the test class, the testCreate() must run before testList().
By default, JUnit executes tests in a specific order but is not predictable. JUnit 5 allows programmers to override that default to run tests in a determined order like alphanumeric order or numeric order, etc.
Let's get started, and I hope you learn a lot from this tutorial.
Alphanumeric Order
The Alphanumeric order sorts test methods alphanumerically.
Just specify the @TestMethodOrder(Alphanumeric.class) annotation for the test class to run tests in the order of test method names by alphanumeric order. We will create four tests named testB, testA, test2, test1. According to Alphanumeric order, we expect tests to run in the order of test1, test2, testA, testB.
Test:
package com.aditya04848.junit.helper;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(MethodOrderer.Alphanumeric.class)
public class alphaNumericOrderTests {
@Test
public void testB() {
System.out.println("test B");
assertTrue(true);
}
@Test
public void testA() {
System.out.println("test A");
assertTrue(true);
}
@Test
public void test2() {
System.out.println("test 2");
assertTrue(true);
}
@Test
public void test1() {
System.out.println("test 1");
assertTrue(true);
}
}Output:
JUnit Output:
Console Output:
In the console, we can see the order of the executed tests.









