Why does JUnit run my test methods in a weird but consistent order?
I was writing some basic test cases in Java using IntelliJ and noticed something strange:
- The tests don’t run in the order I wrote them
- The order is NOT alphabetical
- But it’s also NOT random — it stays the same every time
Example: I defined tests in this order:
simpleTest
validMobileNo
missingPlus
tooShort
tooLong
nullNumber
emptyNumber
But execution order looks like:
tooShort
nullNumber
emptyNumber
tooLong
simpleTest
validMobileNo
missingPlus
So I went down a rabbit hole to understand why 😅
What I found:
- JUnit does NOT reorder tests by default (unless you use u/TestMethodOrder)
- It relies on reflection (
getDeclaredMethods()) - Reflection returns methods in the order they exist in the compiled
.classfile - The
.classfile order is determined by the Java compiler — not necessarily your source code order
So effectively:
Execution order = JVM reflection order = class file method order
That’s why:
- It looks random ❌
- But is consistent every run ✅
Key takeaway:
If you care about order, don’t rely on this behavior. Use:
[u/TestMethodOrder](/u/TestMethodOrder)(MethodOrderer.OrderAnnotation.class)
class MainTest {
[u/Test](/u/Test)
void simpleTest() {
int result = 2 + 3;
assertEquals
(5, result);
}
[u/Test](/u/Test)
void validMobileNo_ShouldReturnTrue(){
boolean result = Main.
isValidMobileNo
("+919313625910");
assertTrue
(result);
}
[u/Test](/u/Test)
void missingPlus_ShouldReturnFalse(){
boolean result = Main.
isValidMobileNo
("919313625910");
assertFalse
(result);
}
[u/Test](/u/Test)
void tooShort_ShouldReturnFalse(){
boolean result = Main.
isValidMobileNo
("+7408512");
assertFalse
(result);
}
[u/Test](/u/Test)
void tooLong_ShouldReturnFalse(){
boolean result = Main.
isValidMobileNo
("+96388574788968570");
assertFalse
(result);
}
[u/Test](/u/Test)
void nullNumber_ShouldReturnFalse(){
boolean result = Main.
isValidMobileNo
(null);
assertFalse
(result);
}
this is my java class
void emptyNumber_ShouldReturnFalse(){
boolean result = Main.
isValidMobileNo
("");
assertFalse
(result);
}
}
package org.example;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.engine.*;
import static org.junit.jupiter.api.Assertions.*;
//import org.junit.jupiter.api.TestMethodOrder;
//import org.junit.jupiter.api.MethodOrderer;
//@TestMethodOrder(MethodOrderer.MethodName.class)
class MainTest {
[u/Test](/u/Test)
void simpleTest() {
int result = 2 + 3;
assertEquals(5, result);
}
[u/Test](/u/Test)
void validMobileNo_ShouldReturnTrue(){
boolean result = Main.isValidMobileNo("+911234567890");
assertTrue(result);
}
[u/Test](/u/Test)
void missingPlus_ShouldReturnFalse(){
boolean result = Main.isValidMobileNo("911234567890");
assertFalse(result);
}
[u/Test](/u/Test)
void tooShort_ShouldReturnFalse(){
boolean result = Main.isValidMobileNo("+7408512");
assertFalse(result);
}
[u/Test](/u/Test)
void tooLong_ShouldReturnFalse(){
boolean result = Main.isValidMobileNo("+96388574788968570");
assertFalse(result);
}
[u/Test](/u/Test)
void nullNumber_ShouldReturnFalse(){
boolean result = Main.isValidMobileNo(null);
assertFalse(result);
}
[u/Test](/u/Test)
void emptyNumber_ShouldReturnFalse(){
boolean result = Main.isValidMobileNo("");
assertFalse(result);
}
}
this is java class
can anyone explain why at execution i found this order and every time it follow same order so how it happens
Posted Apr 2, 2026, 11:23 AM UTC
05