Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Explain dependsOnMethods and dependsOnGroups in TestNG. What happens to a dependent test when the test it depends on fails?

In TestNG, @Test(dependsOnMethods = "login") makes a test run after login() and only if login() passed, and dependsOnGroups does the same for every method in the named groups. If a dependency fails, the dependent test is not run and is reported as SKIPPED, unless we add alwaysRun = true, which turns it into a soft dependency that keeps the order but runs regardless of the outcome.

Why this question is asked

JUnit deliberately has no way for one test to depend on another, so TestNG's dependencies are one of the features that make it different. Interviewers ask about them because they are easy to use and easy to misuse: they decide both the order of tests and whether a test runs at all, and the skipped results they produce are often misread in CI reports. In this answer, we will recap how TestNG orders tests, see dependsOnMethods in action, follow what happens when the dependency fails, look at soft dependencies with alwaysRun, move on to dependsOnGroups, and finish with what TestNG does with cycles and missing dependencies.

1A Quick Recap of Test Order in TestNG

TestNG does not promise any particular order for the test methods of a class. In practice the order may look stable, but it is an implementation detail, and it can change between versions or when parallel execution is turned on. If a test needs another one to run first, we must say so.

There are three ways to say it. The priority attribute, as in @Test(priority = 1), sorts methods by number but creates no link between them. The preserve-order setting in testng.xml keeps classes and methods in the order they are listed in the suite file. And dependencies, the subject of this answer, give both an order and a condition: the dependent test runs after its dependency, and only if it passed.

2Declaring a Dependency on Another Method

We name the method we depend on in the dependsOnMethods attribute. The value is the method name as a string, and we can list several.

Java
public class AccountFlowTest {
@Test
public void login() {
session = client.login("ana", "secret");
assertNotNull(session); // passes
}
@Test(dependsOnMethods = "login")
public void openDashboard() {
assertEquals(client.dashboard(session).title(), "Dashboard"); // runs after login()
}
@Test(dependsOnMethods = {"login", "openDashboard"})
public void logout() {
client.logout(session); // runs after both
}
}

TestNG builds a graph from these attributes and runs login() first, then openDashboard(), then logout(). Notice that the dependency is a string, so the compiler cannot check it. If we rename login() and forget the attribute, TestNG reports an error at startup that the dependent method depends on a method that does not exist. TestNG also has an ignoreMissingDependencies attribute that silences that error, which is rarely what we want.

3What Happens When the Dependency Fails

Now suppose the login service is down and login() throws an exception. TestNG marks login() as FAILED. It does not run openDashboard() or logout() at all, and it reports both as SKIPPED.

Java
// login() throws: java.net.ConnectException: Connection refused
// TestNG result for each method:
// login FAILED
// openDashboard SKIPPED (depends on login, which failed)
// logout SKIPPED (depends on login and openDashboard)
//
// Summary line: Total tests run: 3, Passes: 0, Failures: 1, Skips: 2
Step throughHow TestNG decides whether to run a dependent test
  1. Run the dependency firstlogin()
  2. Did the dependency pass?
    YesRun the dependent testNoCheck alwaysRun
  3. Is alwaysRun = true?
    YesRun it anyway (soft dependency)NoReport the test as SKIPPED

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

The reason for skipping rather than failing is that the dependent tests have no meaning without their dependency. If we ran openDashboard() without a session, it would fail with a confusing NullPointerException and hide the real cause. A skip tells the reader that the test was not tried, and the one failure points at the real problem.

But there is a catch: a skip is not a pass. Many CI dashboards show only failures in red, so a suite with 1 failure and 37 skips can look nearly healthy. We should always read the skip count together with the failure count.

4Soft Dependencies With alwaysRun = true

Sometimes we want the order but not the condition. A typical example is a test that cleans up or collects diagnostics: it should come after the main test, but it should run even if the main test failed. For this, TestNG lets us add alwaysRun = true to the dependent test.

Java
@Test
public void placeOrder() { ... } // fails
@Test(dependsOnMethods = "placeOrder", alwaysRun = true)
public void captureServerLogs() { ... } // still runs, after placeOrder()

This is called a soft dependency. The default, without alwaysRun, is a hard dependency: order plus condition. Notice that a soft dependency still waits for its dependency to finish, so the order is guaranteed in both cases.

5Depending on Whole Groups With dependsOnGroups

A group in TestNG is a label we attach to tests, as in @Test(groups = "init"). With dependsOnGroups, a test waits until every method of the listed groups has run, and it runs only if all of them passed.

Java
@Test(groups = "init")
public void startServer() { ... } // passes
@Test(groups = "init")
public void loadReferenceData() { ... } // fails
@Test(dependsOnGroups = "init")
public void searchProducts() { ... } // SKIPPED: one method of init failed

This is useful when the dependency is a stage rather than a single method, for example all the setup tests of a database or all the smoke checks of a deployment. It also survives renames better, because we depend on a label rather than on a method name. The rule about alwaysRun is the same: with alwaysRun = true the test runs after the group even if part of it failed.

6Cycles and How TestNG Reports Them

Because dependencies form a graph, it is possible to write a loop: a depends on b and b depends on a. TestNG detects this before running anything and fails the run with an error that lists the methods that have cyclic dependencies; the exact wording depends on the version. Nothing in the cycle can ever run, so failing early is the only sensible choice, and the fix is to remove one of the links.

In practice, cycles are rare in small classes but appear in big suites where dependencies cross classes through groups. Keeping dependency chains short and inside one class makes them easy to spot in review.

7Follow-up questions

Can a test depend on a method in another class?

Yes. dependsOnMethods accepts a fully qualified name such as com.acme.LoginTest.login, and dependsOnGroups works across classes naturally because groups are labels. Cross-class dependencies make tests harder to run alone, though, because running one class in the IDE pulls in the other, so most teams keep method dependencies inside one class and use groups for anything broader.

What is the difference between priority and dependsOnMethods?

Priority only sorts. A test with priority 2 runs after a test with priority 1, but it still runs if that test fails, and the two have no relationship in the reports. dependsOnMethods sorts and also adds a condition, so a failed dependency causes a skip. If we only need order, priority or preserve-order is lighter; if a test is meaningless without another, a dependency says so explicitly.

Does a skipped dependent test fail the build?

By default, Maven Surefire fails the build on failures and errors, not on skips. The failing dependency itself already fails the build, so in most cases that is enough. The danger is a dependency that is skipped for another reason, for example a failed configuration method, which can turn a whole chain into skips with no failure at all. Watching the skip count in CI catches that.

8Conclusion

In this answer, we saw that TestNG does not guarantee test order on its own, and that dependsOnMethods and dependsOnGroups give a test both an order and a condition. When a dependency fails, the dependent test is reported as SKIPPED rather than run, unless alwaysRun = true makes it a soft dependency. dependsOnGroups waits for every method in the listed groups, missing dependencies are reported at startup, and cycles are detected before any test runs.

What to say in the interviewAbout 80 seconds aloud

Open with

dependsOnMethods tells TestNG that a test only makes sense after another test has passed. So with @Test(dependsOnMethods = "login") on openDashboard, TestNG runs login first, and if login passes it runs openDashboard.

Then, if they want more

If login fails, TestNG does not run openDashboard at all and reports it as skipped, which is better than letting it fail with a confusing null pointer and hiding the real cause.

If I want the order but not the condition, for example for a cleanup or log collection step, I add alwaysRun = true, which makes it a soft dependency: it still waits for login, but runs whatever the outcome. dependsOnGroups is the same idea for a label: the test waits for every method in the listed groups, and runs only if all of them passed.

Because dependencies also define order, they are one of the few ways to get a guaranteed order in TestNG, along with priority, which only sorts, and preserve-order in the suite file. TestNG checks the graph at startup, so a cycle or a dependency on a method that does not exist is reported as an error before anything runs.

In CI I always watch the skip count, because one failed dependency can turn a long chain into skips that look harmless on a dashboard.

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 "Explain dependsOnMethods and dependsOnGroups in TestNG. What happens to a dependent test...": https://scrivio-interview-library.netlify.app/java/testng/81b781218b/