Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

An end-to-end TestNG suite uses a chain of 40 dependent tests: create user, log in, create order, pay, and so on. Discuss the trade-offs and how you would restructure it.

A 40-step dependency chain reuses setup cheaply and reads like a user journey, but one failure near the start skips everything after it, hides other defects, and makes the tests impossible to run alone or in parallel. We would restructure it into independent tests that create the state they need through APIs or fixtures, keep two or three real end-to-end journeys, keep any remaining dependencies short and coarse through groups, and share expensive data through suite-level setup rather than through test order.

Why this question is asked

Long chains of dependent tests usually start with good intentions. Someone writes create user, then log in, then create order, and each new test reuses what the previous one left behind because it is quicker than setting things up again. Two years later the suite is 40 links long, takes 25 minutes, and a single broken login step turns 39 tests into skips. Interviewers ask this scenario because it tests whether we can see past the convenience of the chain to what it does to feedback and debugging. In this answer, we will look at how such a chain is built, weigh what it gives and what it costs, and then restructure it step by step, keeping the parts that are worth keeping.

1How the Chain Looks Today

In TestNG, each test in the chain uses dependsOnMethods to name the step before it, and the steps pass data to each other through fields of the test class. TestNG creates one instance of the class and runs every method on it, so a field set in one test is visible in the next.

Java
public class CheckoutJourneyTest {
private String userId, token, orderId;
@Test public void createUser() { userId = api.createUser("ana@example.com"); }
@Test(dependsOnMethods = "createUser")
public void logIn() { token = api.login(userId, "pw"); }
@Test(dependsOnMethods = "logIn")
public void createOrder() { orderId = api.createOrder(token, "SKU-1"); }
@Test(dependsOnMethods = "createOrder")
public void pay() { assertEquals(api.pay(token, orderId), "PAID"); }
// ... 36 more steps: ship, invoice, refund, review, close account
}

Notice that the fields are the real dependency. The attribute dependsOnMethods only makes TestNG respect the order the fields need. Each test is less a test than a step of one big procedure.

2What the Chain Gives Us

The chain has real advantages, and we should name them before changing it. Setup is reused: we create one user and one order and run 40 checks against them, instead of creating 40 users. The flow is easy to read, because it matches how a customer moves through the product, and a product manager can follow the test names like a script. It also checks that the steps work together, which isolated tests do not prove on their own.

3What the Chain Costs Us

The first cost is hidden defects. If logIn() fails at step 2, TestNG reports 1 failure and 38 skips. Any bug in invoicing or refunds stays invisible until login is fixed, and then appears a day later as a new surprise. The second cost is that no test can run alone. A developer working on refunds cannot run the refund test in the IDE; TestNG pulls in the 30 steps before it, so every debug cycle takes the full 20 minutes.

The third cost is time. A chain is sequential by nature, so parallel execution cannot help: if the 40 steps take about 30 seconds each, the class takes 20 minutes whatever the thread count. Finally, failures are hard to read. A failure at step 31 may be caused by odd state left by step 12, so the failing test is not where the bug is.

One early failure in the chain
StepTestResult
1createUserPASSED
2logInFAILED: 503 from auth service
3createOrderSKIPPED
4paySKIPPED
5 to 40ship, invoice, refund, ...SKIPPED (38 in total)
One failure, 38 skips, and no information about any of the later features.

4Restructuring: Each Test Builds the State It Needs

The main change is to make each test independent. Instead of waiting for the previous test to create a user and an order, a test creates exactly the state it needs, quickly, through the application's APIs or through fixtures that write directly to the database. The important point is that this setup is not the thing under test, so it can take a shortcut that a user could not.

Java
public class RefundTest {
private TestData data; // small helper over the public API and the test database
@BeforeMethod
public void setUp() {
data = new TestData(api);
}
@Test
public void refundsAPaidOrder() {
PaidOrder order = data.paidOrder("SKU-1"); // user, login, order, payment: about 300 ms
Refund refund = api.refund(order.token(), order.id());
assertEquals(refund.status(), "REFUNDED"); // runs alone, in any order
}
}

Notice that data.paidOrder() hides four steps that used to be four tests. It calls the API directly, with no browser, so it takes well under a second. Now the refund test can run alone in the IDE, in parallel with every other test, and if login breaks, it fails with a clear setup error instead of being silently skipped. Each feature reports its own result.

We would not throw away the journey completely, though. We keep two or three real end-to-end journeys, such as sign up and buy, and buy and refund, as single tests with several steps inside, because they are the only proof that the pieces fit together. The detailed checks of each step move into the independent tests.

5Where Dependencies Stay, Keep Them Short

Some dependencies are still useful. A deployment smoke check that the service is up and reachable is a good precondition for everything else: if it fails, running 500 tests that will all time out is a waste. For that we use groups rather than long method chains, and we keep them coarse.

Java
@Test(groups = "env-ready")
public void serviceIsHealthy() { assertEquals(api.health(), "UP"); }
@Test(dependsOnGroups = "env-ready")
public void refundsAPaidOrder() { ... } // waits for the health check only, not for 30 other steps

One link that says the environment works is cheap and honest. Forty links that pass state through fields are what we are removing.

6Sharing Expensive Data the Right Way

Sometimes setup really is expensive, for example loading a product catalogue of 50,000 items or creating a merchant account that needs manual approval in a test system. The answer is not test order but suite-level setup. A @BeforeSuite or @BeforeClass method prepares the shared, read-only data once, and every test reads it without changing it. Data that a test changes, such as an order or a balance, is always created fresh by that test.

With that split, the suite gets the reuse the chain promised without the coupling. In a typical rewrite of a chain like this, the 40 steps become about 35 independent tests and 3 journey tests, the class runs in 4 or 5 minutes across 8 threads instead of 20, and a failure in login shows as a failure in the login tests and in setup, while the refund and invoice tests still report on their own features where they can.

Step throughRestructuring the chain
  1. List the steps and the state each one needsuser, token, order, payment
  2. Build fixtures and API helpers for that statedata.paidOrder() and similar
  3. Turn each check into an independent testruns alone and in parallel
  4. Keep two or three real journey testsproof that the pieces fit
  5. Share read-only data in @BeforeSuitecatalogue, reference data
  6. Enable parallel runs and watch for flakinessraise thread-count gradually

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

7Follow-up questions

Is it wrong to share a user across tests if setup is slow?

Sharing is fine for data that no test changes, such as a read-only reference account. The problem is shared mutable state: if one test changes the user's address and another reads it, they depend on each other through the data even without dependsOnMethods. Shared data should be created once in suite setup and treated as read-only; anything a test modifies should be its own.

How do you convince the team to give up the chain?

With their own numbers. We count how often one failure caused more than ten skips in the last month, and how long a developer waits to debug one step in the IDE. Then we rewrite one slice, for example the refund steps, and show it running alone in seconds and in parallel. A working slice persuades better than a design document.

What if the API does not offer a way to create the state directly?

Then we add a test-only route or write fixtures straight into the database, protected so they exist only in test environments. Many teams expose a small test-support API for this. It is a small investment compared with the cost of a 40-step chain, and it makes every other test easier to write.

8Conclusion

In this answer, we saw that a long chain of dependent tests reuses setup and reads like a journey, but one early failure skips everything after it, no test can run alone, and nothing can run in parallel. We restructured it into independent tests that build their own state through API helpers and fixtures, kept a few real journey tests as proof that the steps fit together, kept only short, coarse dependencies through groups, and moved expensive read-only data into suite-level setup.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start by acknowledging what the chain does well: it reuses one user and one order for 40 checks, and it reads like a real customer journey.

Then, if they want more

But it has serious costs. If step 2, login, fails, TestNG skips the other 38, so every later defect is hidden until login is fixed. No test can run alone, so a developer debugging refunds waits for the whole chain each time. And it cannot run in parallel, so the class takes as long as all 40 steps added together.

To restructure it, I would build a small test-data helper over the API, so a test can say give me a paid order and get it in a few hundred milliseconds without a browser.

Then each check becomes an independent test that creates its own state, runs alone in the IDE and in parallel in CI, and fails for its own reason. I would keep two or three genuine end-to-end journeys as single tests, because they prove the parts fit together.

Where a dependency still helps, like a health check that the environment is up, I would express it as one coarse group dependency, not a chain. And expensive read-only data, like a large catalogue, goes into @BeforeSuite setup, while anything a test modifies is created by that test.

I would roll it out one slice at a time and show the time saved.

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.
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 "An end-to-end TestNG suite uses a chain of 40 dependent tests: create user, log in...": https://scrivio-interview-library.netlify.app/java/testng/2db6da44e9/