Quick revision
The short answer
A company test standard should forbid test dependencies in unit and integration tests, where every test must be independent and runnable alone, and allow them only in a small number of long journey tests where setup is very costly and the flow itself is what we are testing. Where they are allowed, the standard should require that skips caused by a failed dependency are visible in reports, give teams fixtures and API helpers so they do not need chains for setup, and review every exception periodically.
What to remember
In this answer, we saw that a test dependency couples tests by order and usually by state. A sound standard forbids it in unit, integration and contract tests, which must run alone, in any order and in parallel, and allows it only for short journey tests where setup is costly and the flow is the subject. It requires skipped tests to be visible, enforces the rule with a shared listener, gives teams fixtures and API helpers so they do not need chains, and reviews each exception before it expires.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A company test standard should forbid test dependencies in unit and integration tests, where every test must be independent and runnable alone, and allow them only in a small number of long journey tests where setup is very costly and the flow itself is what we are testing. Where they are allowed, the standard should require that skips caused by a failed dependency are visible in reports, give teams fixtures and API helpers so they do not need chains for setup, and review every exception periodically.
Why this question is asked
A standard is a decision we make once so that 50 teams do not have to argue about it 50 times. Test dependencies, such as TestNG's dependsOnMethods or JUnit's @TestMethodOrder with tests that rely on each other, are a good subject for one, because individual teams adopt them for convenience and the cost appears later, in skipped tests, slow pipelines and tests that cannot run in parallel. Interviewers ask this at a senior level to see whether we can write a rule that is strict where it must be, flexible where it should be, and enforceable. In this answer, we will recap what a dependency does, explain where we forbid it and why, define the narrow case where we allow it, and then cover reporting, the helpers that make the rule easy to follow, and how exceptions are reviewed.
1A Quick Recap of What a Dependency Does
In TestNG, @Test(dependsOnMethods = "createOrder") means the test runs after createOrder() and only if it passed; otherwise it is reported as SKIPPED. dependsOnGroups does the same for every test in a group. JUnit Jupiter has no dependency feature, but it can order tests with @TestMethodOrder and @Order, and teams sometimes combine that with a shared field or a per-class lifecycle to pass state from one test to the next, which creates the same coupling without the skip.
So a dependency couples two tests in two ways: by order, because one must run before the other, and usually by state, because the later test uses something the earlier one created. The standard has to deal with both.
2Where We Forbid Them: Unit and Integration Tests
Unit tests check one class or a small group of classes, and integration tests check a service with its database or a message broker. For both, the standard says that every test must be independent: it creates the state it needs, it can run alone from the IDE, and it passes in any order and in parallel with any other test.
The reason is that these tests make up the bulk of the suite, often 90 percent of it or more, and they are what developers run dozens of times a day. If one of them depends on another, the developer cannot run it alone, the CI system cannot shard it freely across machines, and a failure in one place turns into skips elsewhere. Independence is also what makes test selection and parallel execution possible later, so breaking it here limits every improvement we might want to make to the pipeline.
3Where We Allow Them: Costly Journey Tests
There is one honest use case. A journey test walks through a business flow end to end, such as open an account, verify identity, deposit, transfer, close, against a deployed system. When each step is expensive, for example identity verification in a partner's sandbox takes two minutes, and when the flow itself is what we want to prove, splitting it into independent tests would repeat the expensive steps for every check.
The standard allows dependencies here under conditions we can check: the tests carry a journey group, the chain is short, say at most 6 steps, the dependencies stay inside one class, and the class has a named owner. A typical company has perhaps 10 to 30 such journeys in total, compared with thousands of unit and integration tests.
| Kind of test | Dependencies | Reason |
|---|---|---|
| Unit | Forbidden | Must run alone, in any order, in milliseconds |
| Integration | Forbidden | Must shard and run in parallel in CI |
| Contract | Forbidden | Each contract is checked on its own |
| Journey (end to end) | Allowed, at most 6 steps, one class | Setup is costly and the flow is the subject |
| Environment smoke check | Allowed as one group precondition | Avoids running 500 tests against a dead system |
4Making Skipped Tests Visible
A failed dependency produces skips, and skips are easy to overlook: many dashboards colour only failures. So the standard requires that skips caused by a failed dependency appear in reports with their reason, and that the pipeline shows the skip count next to the failure count. A useful extra rule is that a run with skips but no failures is treated as suspicious, because it usually means a configuration method or a dependency failed in a way nobody noticed.
5Enforcing the Rule in the Build
A standard that lives only in a wiki is not followed. We enforce this one with a small listener in the shared test library that every repository uses. Before the suite runs, it looks at every test method and stops the run if a method outside the journey group declares a dependency.
Notice the call to Exceptions.allowed(m). It reads a small file of approved exceptions, each with an owner, a reason and an expiry date. The listener is registered through the shared library, so teams get the check without writing anything. For JUnit suites, a similar check can be written as an ArchUnit rule or a build step that flags @TestMethodOrder outside the journey packages.
6Giving Teams Something Better Than Chains
People use dependency chains because setup is hard. If creating a paid order takes 40 lines of code, chaining the order test to the checkout test looks reasonable. So the standard comes with tools: a test-data library that creates common states through APIs in one call, such as testData.paidOrder() or testData.verifiedCustomer(), container-based fixtures for databases, and builders for request objects. When the independent version of a test is as easy to write as the dependent one, the rule stops feeling like a restriction.
7Reviewing Exceptions Over Time
Finally, exceptions are reviewed on a schedule, for example every quarter. Each exception has an expiry date, and the listener rejects it once the date passes, so the owner must either renew it with a reason or remove the dependency. The review also looks at the numbers: how many journey tests exist, how often their chains skip, and whether new helpers would let us retire some of them. A rule that is reviewed stays credible; a list of exceptions that only grows is how standards die.
- Is it a unit, integration or contract test?YesReject: offer fixtures and helpersNoNext question
- Is setup very costly and the flow the subject?YesNext questionNoReject: make tests independent
- At most 6 steps in one class?YesAllow with journey group and ownerNoAsk to split the journey
- Record the exception with an expiry datereviewed every quarter
4 steps in order. Play them, or step through them one at a time.
8Follow-up questions
Is ordering tests without passing state also forbidden?
Ordering alone is less harmful, but in unit and integration tests it usually signals hidden shared state, because independent tests should not care about order. The standard therefore forbids relying on order there too. A good way to prove independence is to run the suite in random order in CI, which JUnit Jupiter supports with MethodOrderer.Random, so hidden order dependencies show up as failures.
What about tests that must run after deployment, such as smoke tests?
A single coarse precondition is fine: one health-check group that every post-deployment test depends on, so a dead environment produces one clear failure instead of hundreds of timeouts. That is a different thing from a chain that passes state, and the standard can say so explicitly.
How do you introduce the rule in repositories that already break it?
We start the listener in reporting mode, which logs violations without stopping the run, publish the count per repository, and give teams a deadline, for example two quarters, with help from the helpers library. After the deadline, the listener switches to enforcing mode, and any remaining cases become recorded exceptions with owners.
9Conclusion
In this answer, we saw that a test dependency couples tests by order and usually by state. A sound standard forbids it in unit, integration and contract tests, which must run alone, in any order and in parallel, and allows it only for short journey tests where setup is costly and the flow is the subject. It requires skipped tests to be visible, enforces the rule with a shared listener, gives teams fixtures and API helpers so they do not need chains, and reviews each exception before it expires.
Open with
My default is to forbid test dependencies in unit and integration tests. Those make up most of the suite, developers run them constantly, and they only work well if every test is independent: it creates its own state, runs alone from the IDE, and passes in any order and in parallel.
Then, if they want more
A dependency breaks all of that, and it turns one failure into a string of skips. I would allow dependencies in one narrow case: long journey tests against a deployed system, where setup is genuinely expensive, like a two-minute identity check in a partner sandbox, and the flow itself is what we are proving.
Even there I would keep chains short, say six steps, inside one class, tagged as journey tests and owned by a named team. Where dependencies are allowed, skips must be visible in reports with their reason, and I would treat a run with skips and no failures as suspicious.
I would enforce the rule with a listener in our shared test library that stops the run when a non-journey test declares a dependency, and I would back it with a test-data library so creating a paid order or a verified customer is one call, because people build chains when setup is painful.
Finally, each exception gets an owner and an expiry date, and we review them every quarter so the list does not just grow.
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, Mockito documentation
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 "When would you allow test dependencies (TestNG dependsOnMethods or ordered tests) in a...": https://scrivio-interview-library.netlify.app/java/testng/e9f1442e17/