Scrivio Interview library
LeadershipAdvanced6 min read · updated 12 November 2026

Your teams frequently use @TestMethodOrder to make tests depend on each other's results. What standard would you set and why?

I would set the standard that every test is independent: it must pass when run alone, in any order and in parallel. Ordered tests hide their setup in earlier tests, so one failure cascades into many and the real cause is hard to find. @TestMethodOrder is allowed only for explicit end-to-end scenario tests, kept in a separate suite and named as such, and we enforce the standard by running the normal suites in random order regularly with MethodOrderer.Random.

Why this question is asked

When teams start chaining tests, the first test creates a customer, the second places an order for that customer, the third ships it, it feels efficient: no repeated setup, and the tests read like a story. The cost appears later, as cascading failures, tests that cannot be run alone and suites that can never be parallelized. Interviewers ask this leadership question to see whether we can set a standard, explain it so teams accept it, and leave room for the legitimate cases. In this answer, we will see what @TestMethodOrder does, why dependent tests hurt, what standard to set and where the exception is, and how to enforce it and help teams migrate.

1What @TestMethodOrder Does

By default JUnit runs the methods of a class in an order that is deterministic but deliberately not obvious, so that tests do not come to depend on it. @TestMethodOrder chooses an explicit orderer: MethodOrderer.OrderAnnotation with @Order(1), @Order(2) and so on, MethodName, DisplayName, or Random. Ordering itself is harmless; the problem is what teams use it for, which is making a later test rely on what an earlier test left behind.

Java
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class CustomerFlowTest {
static Long customerId; // shared between tests through a static field
@Test @Order(1) void createsCustomer() { customerId = api.createCustomer("Ada"); }
@Test @Order(2) void placesOrder() { api.placeOrder(customerId, "book"); } // needs test 1
@Test @Order(3) void shipsOrder() { assertEquals("SHIPPED", api.ship(customerId)); } // needs 1 and 2
}
// If createsCustomer fails: placesOrder and shipsOrder also fail, with NullPointerException

2Why Dependent Tests Hurt

The first cost is cascading failures. If test 1 breaks, tests 2 and 3 fail too, and the report shows three failures, two of them with misleading messages such as a NullPointerException. In a class of 15 chained tests, one bug shows up as 15 red tests, and the person on call has to work out which one is real.

The second cost is that tests cannot run alone. A developer who wants to debug shipsOrder must run the whole class, and an IDE re-run of the failing test fails for a different reason. The third is that setup becomes invisible: to understand what shipsOrder tests, we must read two other tests. And the fourth is that the suite can never run in parallel or be split across CI agents, because the tests are really one long test in pieces.

Step throughHow one failure cascades in a chained class
  1. Test 1: create customerfails: validation bug
  2. Test 2: place orderfails: customerId is null
  3. Test 3: ship orderfails: no order found
  4. Report3 failures, only 1 real cause

4 steps in order. Play them, or step through them one at a time.

3The Standard to Set

The rule I would write is short: every test must pass when run alone, in any order, and in parallel with others. Each test creates the data it needs, through a helper method, a builder or a fixture extension, and does not read state left by another test. Shared setup goes into @BeforeEach or a test fixture, where it is visible and runs for every test.

The story the team wanted is still possible, just written differently: each test sets up the state before its step with a helper, for example givenCustomerWithOrder(), and then tests only that step. The setup cost is usually tiny compared with the time lost to cascading failures.

Java
class ShippingTest {
@Test void shipsPaidOrder() {
Long customerId = fixtures.customer("Ada"); // own setup, visible here
Long orderId = fixtures.paidOrder(customerId, "book");
assertEquals("SHIPPED", api.ship(orderId)); // passes alone and in any order
}
}

4Where Ordering Is Allowed

There is a legitimate case: an end-to-end scenario test that deliberately walks through a user journey against a deployed system, such as sign up, buy, cancel, refund, where creating fresh state for each step is impossible or very slow. For those, ordering is allowed under three conditions. They live in a separate suite or source set, such as src/e2eTest, run in their own CI stage, and are named as scenarios, for example CheckoutJourneyScenario, so nobody mistakes them for unit tests. Each class is one scenario with one clear purpose. And they are kept few, because they are slow and fragile by nature.

JUnit also helps the scenario case: with @TestInstance(Lifecycle.PER_CLASS) the scenario can keep its state in instance fields rather than statics, and a failed step can make the later steps abort instead of producing misleading failures.

5Enforcing the Standard and Migrating

Standards that are not checked fade. I would add two checks. The first runs the normal suites in random order on a schedule, for example nightly, by setting the default orderer to MethodOrderer.Random and ClassOrderer.Random and logging the seed, so any hidden dependency shows up as a failure that can be replayed. The second is a simple build rule, for example an ArchUnit test, that forbids @TestMethodOrder outside the scenario source set.

For existing chained classes, I would not demand a rewrite overnight. Teams list them, the worst offenders, those that fail most often in CI, are rewritten first with fixture helpers, and the rest are either rewritten or moved into the scenario suite by an agreed date. Explaining the reason, faster debugging and the possibility of parallel CI, matters more than the rule itself.

Java
# junit-platform.properties for the nightly random-order job
junit.jupiter.testmethod.order.default = org.junit.jupiter.api.MethodOrderer$Random
junit.jupiter.testclass.order.default = org.junit.jupiter.api.ClassOrderer$Random
# the random seed is printed in the log; reuse it to replay a failure

6Follow-up questions

Isn't recreating data in every test slow?

Sometimes slightly, but usually not enough to matter, especially with fixture helpers that insert directly into the database instead of going through the API. And independent tests can run in parallel and be split across agents, which more than pays back the extra setup on a large suite.

What about tests that must share an expensive resource like a container?

Sharing a resource is fine; sharing state in it is the problem. A Testcontainers database can be started once per class or per suite, while each test creates its own rows and cleans up or rolls back. Independence is about data and results, not about starting everything from scratch.

How would you convince a team that likes their ordered tests?

I would show their own numbers: how often one failure in their chained classes produced many red tests, how long debugging took, and how much faster the suite would be in parallel. Then I would help rewrite one class with fixture helpers so they can see it is not much more code.

7Conclusion

In this answer, we saw that @TestMethodOrder is harmless as a feature but harmful when used to make tests depend on each other, because failures cascade, tests cannot run alone, setup becomes invisible and the suite cannot run in parallel. The standard is that every test is independent; ordering is allowed only for explicit end-to-end scenario tests in a separate, clearly named suite. We enforce it with periodic random-order runs using MethodOrderer.Random and a build rule, and migrate existing chains with fixture helpers.

What to say in the interviewAbout 90 seconds aloud

Open with

I would set a simple standard: every test must pass on its own, in any order, and in parallel. Tests that depend on each other through @TestMethodOrder hide their setup in earlier tests.

Then, if they want more

When test one breaks, tests two to fifteen also go red with misleading errors like null pointers, so one bug looks like fifteen, nobody can rerun a single failing test, and the suite can never be parallelized or split across agents.

So each test creates its own data through fixture helpers or builders, and shared setup goes into @BeforeEach where it is visible. I would allow ordering in one case: explicit end-to-end scenario tests that walk through a user journey where fresh state per step is impractical.

Those live in a separate source set and CI stage, are named as scenarios, keep their state in instance fields with the per-class lifecycle, and stay few.

To enforce it, I would run the normal suites nightly in random order using MethodOrderer.Random and ClassOrderer.Random with the seed logged, so hidden dependencies appear and can be replayed, and add an ArchUnit rule that forbids @TestMethodOrder outside the scenario suite.

For existing chained classes, I would rewrite the flakiest ones first and agree a date for the rest, and I would explain the why with the team's own failure data.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
JUnit user guide
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your teams frequently use @TestMethodOrder to make tests depend on each other's results....": https://scrivio-interview-library.netlify.app/java/junit/9b054fa5ce/