JUnit's TestMethodOrder annotation not working
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If @TestMethodOrder is "not working," the first thing to check is whether you are actually running JUnit 5 Jupiter tests. @TestMethodOrder is a JUnit 5 feature. If the class is being executed with JUnit 4 tooling or with the wrong imports, the annotation will not behave the way you expect.
The second thing to check is whether the chosen orderer matches the annotations you used. MethodOrderer.OrderAnnotation only pays attention to methods marked with @Order.
A Correct JUnit 5 Example
This is a working JUnit 5 setup:
If this still appears unordered, the runner or dependency setup is often the real issue.
Common Reasons It Does Not Work
You Are Not Running Jupiter
If your build or IDE is using JUnit 4, @TestMethodOrder will not control execution the way you expect. In JUnit 4, the older feature is @FixMethodOrder.
You Forgot @Order
When you use MethodOrderer.OrderAnnotation, methods without @Order do not automatically become ordered the way many people assume. Add explicit @Order annotations to each method you care about.
Wrong Imports
Make sure your imports come from org.junit.jupiter.api, not from JUnit 4 packages or mixed test libraries.
Build Tool or IDE Uses the Wrong Engine
If Maven, Gradle, or the IDE is still running the Vintage engine or older test infrastructure, the class may not be executed as a Jupiter test class at all.
Check the Build Configuration
For Maven, the important part is that JUnit Jupiter is on the classpath:
For Gradle:
Without useJUnitPlatform(), Gradle may not run Jupiter tests the way you expect.
If You Are Actually on JUnit 4
Then @TestMethodOrder is simply the wrong feature. Use the JUnit 4 equivalent instead:
That is not interchangeable with JUnit 5 annotations.
Ordered Tests Are Usually a Warning Sign
Even when @TestMethodOrder works correctly, needing a strict order often means tests are sharing state or depending on side effects from earlier methods. That can make the suite brittle in IDE runs, parallel execution, and future refactors.
A small ordered sequence is sometimes acceptable for integration-style scenarios, but unit tests should normally be independent and runnable in any order.
A Quick Diagnostic Checklist
If ordering still looks wrong, check these quickly:
- import package is
org.junit.jupiter.api - class is really executed by the Jupiter engine
- '
useJUnitPlatform()is enabled for Gradle' - '
@Orderis present when usingOrderAnnotation' - no old runner, extension, or IDE configuration is silently using JUnit 4 behavior
Common Pitfalls
- Mixing JUnit 4 and JUnit 5 imports in the same class.
- Forgetting
@Orderwhile usingMethodOrderer.OrderAnnotation. - Running tests from an IDE configuration that does not use the JUnit 5 platform.
- Assuming method order should be relied on heavily. Ordered tests are often a design smell.
- Expecting
@TestMethodOrderto fix tests that share mutable state incorrectly.
Summary
- '
@TestMethodOrderis a JUnit 5 Jupiter feature.' - Use the correct Jupiter imports and a matching orderer.
- '
MethodOrderer.OrderAnnotationrequires explicit@Orderannotations.' - Make sure Maven, Gradle, or your IDE is actually running tests on the JUnit platform.
- If you are still on JUnit 4, use
@FixMethodOrderinstead.
Related reading
- JVM initial CPU spike in a Docker container
- JVM option -Xss - What does it do exactly?
- JVM signal chaining SIGPIPE
- JWT decoding with Spring Security
- Kafka Are there are examples on how to use Mockito for unit testing Kafka?
- Kafka consumer unit test with Avro Schema registry failing
- jython multithreading
- Kafka - Could not find a 'KafkaClient' entry in the JAAS configuration java

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.