JUnit
@TestMethodOrder
testing
Java
annotations

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.

Browse interview questions

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:

java
1import org.junit.jupiter.api.MethodOrderer;
2import org.junit.jupiter.api.Order;
3import org.junit.jupiter.api.Test;
4import org.junit.jupiter.api.TestMethodOrder;
5
6@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
7class AccountServiceTest {
8
9    @Test
10    @Order(1)
11    void createAccount() {
12        System.out.println("create");
13    }
14
15    @Test
16    @Order(2)
17    void updateAccount() {
18        System.out.println("update");
19    }
20
21    @Test
22    @Order(3)
23    void deleteAccount() {
24        System.out.println("delete");
25    }
26}

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:

xml
1<dependency>
2    <groupId>org.junit.jupiter</groupId>
3    <artifactId>junit-jupiter</artifactId>
4    <version>5.11.3</version>
5    <scope>test</scope>
6</dependency>

For Gradle:

groovy
1dependencies {
2    testImplementation 'org.junit.jupiter:junit-jupiter:5.11.3'
3}
4
5test {
6    useJUnitPlatform()
7}

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:

java
1import org.junit.FixMethodOrder;
2import org.junit.Test;
3import org.junit.runners.MethodSorters;
4
5@FixMethodOrder(MethodSorters.NAME_ASCENDING)
6public class LegacyTest {
7    @Test
8    public void test1_create() {}
9
10    @Test
11    public void test2_update() {}
12}

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'
  • '@Order is present when using OrderAnnotation'
  • 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 @Order while using MethodOrderer.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 @TestMethodOrder to fix tests that share mutable state incorrectly.

Summary

  • '@TestMethodOrder is a JUnit 5 Jupiter feature.'
  • Use the correct Jupiter imports and a matching orderer.
  • 'MethodOrderer.OrderAnnotation requires explicit @Order annotations.'
  • Make sure Maven, Gradle, or your IDE is actually running tests on the JUnit platform.
  • If you are still on JUnit 4, use @FixMethodOrder instead.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.