Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

List the TestNG configuration annotations from @BeforeSuite to @AfterMethod in the order they run, and say at what scope each one runs.

TestNG's configuration annotations follow its nesting of suite, test, class and method: @BeforeSuite runs once per suite, @BeforeTest once per <test> tag in testng.xml, @BeforeGroups before the first method of a listed group runs, @BeforeClass once per test class instance, and @BeforeMethod before every test method. The matching @After annotations run in the reverse order. A <test> is a container of classes, not a single test method, and when a @Before method fails, the tests that depend on it are reported as SKIPPED rather than FAILED.

Why this question is asked

TestNG is still common in enterprise Java, especially in older suites and in UI automation, and its lifecycle has more levels than JUnit's. Misreading those levels leads to setup running too often, too rarely, or not at all, and to reports full of skipped tests that nobody understands. Interviewers ask this question to check that we know each level's scope, the confusing meaning of <test>, and what happens when setup fails. In this answer, we will recap how a TestNG run is structured, go through the annotations in order with a runnable example, look at groups and alwaysRun, and finish with what a failing @BeforeClass does.

1A Quick Recap of a TestNG Run's Structure

A TestNG run is described by a testng.xml file, or by the build tool generating an equivalent one. At the top is a suite, the whole run. Inside the suite are one or more <test> elements. Inside each <test> are classes, and inside each class are test methods. Groups are labels on methods, such as smoke or db, and cut across this tree.

The word test is the trap. For someone coming from JUnit, a test is one method. In testng.xml, <test> is a named container of classes, for example all the API classes run against one environment. So @BeforeTest does not run before each test method; it runs once before all the classes in a <test> element.

XML
<suite name="Shop">
<test name="api"> <!-- a container of classes, not a method -->
<classes>
<class name="shop.CartApiTest"/>
<class name="shop.OrderApiTest"/>
</classes>
</test>
</suite>

2The Annotations in the Order They Run

Here is one class with every level, and what it prints when the suite above runs it with two test methods.

Java
public class CartApiTest {
@BeforeSuite public void beforeSuite() { log("before suite"); }
@BeforeTest public void beforeTest() { log("before <test> api"); }
@BeforeClass public void beforeClass() { log("before class"); }
@BeforeMethod public void beforeMethod() { log("before method"); }
@Test public void addsItem() { log("addsItem"); }
@Test public void removesItem() { log("removesItem"); }
@AfterMethod public void afterMethod() { log("after method"); }
@AfterClass public void afterClass() { log("after class"); }
@AfterTest public void afterTest() { log("after <test> api"); }
@AfterSuite public void afterSuite() { log("after suite"); }
}
// before suite
// before <test> api
// before class
// before method
// addsItem
// after method
// before method
// removesItem
// after method
// after class
// after <test> api
// after suite
TestNG configuration annotations
AnnotationRunsTypical use
@BeforeSuiteOnce per suiteGlobal resources, such as a shared server
@BeforeTestOnce per <test> in testng.xmlPer-environment settings for a group of classes
@BeforeGroupsBefore the first method of the listed groupsSetup only some groups need, such as a database
@BeforeClassOnce per test class instanceExpensive setup shared by the class's methods
@BeforeMethodBefore each test methodFresh state for every test
@After... versionsMirror image, in reverse orderRelease what the matching @Before created

Notice the nesting. Suite setup runs once for the whole run, so it is the place for something truly global, such as starting a shared server. <test> setup runs once per <test> element, so it suits per-environment configuration. Class setup runs once per instance of the test class, before its first method. Method setup runs before every test method, which is where per-test state belongs. The @After methods mirror this in reverse: after method, after class, after test, after suite.

3Where @BeforeGroups Fits

Groups do not form a level of the tree; they are labels, so @BeforeGroups behaves a little differently. A method annotated @BeforeGroups("db") runs once, just before the first method in the db group is invoked, and the matching @AfterGroups("db") runs after the last method of that group has finished. It is the right place for setup that only some tests need, so a run of just the smoke group does not pay for starting a database.

It is usually listed between @BeforeTest and @BeforeClass, and that is how most references present it. In a real run, though, because it fires just before the first group method, we will often see it after the @BeforeClass of the class that contains that method. If the exact position matters for our setup, the reliable approach is to print it in a small example on our TestNG version, and better still not to make one setup method depend on another running first.

4AlwaysRun and What It Changes

When we run only some groups, for example mvn test -Dgroups=smoke, TestNG also filters configuration methods by group. A @BeforeMethod that is not in the smoke group would not run, and the smoke tests would fail for lack of setup. alwaysRun = true fixes this for @Before methods: the method runs regardless of which groups are selected.

On @After methods, alwaysRun = true has a second meaning: the method runs even if earlier methods failed or were skipped. That matters for clean-up. An @AfterClass that closes a browser or deletes test data should be marked alwaysRun = true, otherwise a failure earlier in the class leaves the resource open. Note that alwaysRun on a @Before method does not make it run after an earlier configuration failure; that is controlled by the configfailurepolicy setting, which defaults to skip.

Java
@BeforeMethod(alwaysRun = true) public void freshCart() { cart = new Cart(); } // runs for any selected group
@AfterClass(alwaysRun = true) public void closeBrowser() { driver.quit(); } // runs even after failures

5What Happens When a @Before Method Fails

If a configuration method throws, TestNG reports that configuration method as failed, and every test that depended on it as SKIPPED, not FAILED. The reasoning is that those tests never got a fair chance to run, so their result is unknown.

The scope of the damage follows the level. A failing @BeforeMethod skips that test method; with the default configfailurepolicy of skip, the configuration methods that would have run after it in the same class are skipped too, so the following tests in the class are often skipped as well. A failing @BeforeClass skips every test method in that class, and its @AfterClass is skipped unless it has alwaysRun = true. Other classes are not affected. A failing @BeforeSuite skips everything in the suite. Setting configfailurepolicy="continue" in testng.xml makes TestNG keep invoking later configuration methods after a failure, which can reduce cascades but can also hide the first error.

Java
// @BeforeClass beforeClass() throws IllegalStateException("database not reachable")
// FAILED CONFIGURATION: @BeforeClass beforeClass
// SKIPPED: addsItem
// SKIPPED: removesItem
// Tests run: 2, Failures: 0, Skips: 2

Notice that the summary shows zero failures. A CI job that only checks for failures would pass, which is why skips need to be treated as a problem in their own right and why the FAILED CONFIGURATION line is the first thing to read in such a report.

6Follow-up questions

What happens when @BeforeClass throws an exception?

TestNG marks the @BeforeClass method as a failed configuration and reports all test methods of that class as SKIPPED. Remaining configuration methods of the class, including @AfterClass, are skipped too unless they are marked alwaysRun = true, so clean-up code should carry that flag. Tests in other classes still run. The summary shows the skips rather than failures, so the real error is in the configuration failure entry.

Is @BeforeClass the same as JUnit's @BeforeAll?

They are similar in scope, but in TestNG @BeforeClass is an instance method, because TestNG creates one instance of the class and runs all its methods on it. JUnit Jupiter's @BeforeAll is static by default, because Jupiter creates a new instance per test method unless the class uses @TestInstance(PER_CLASS).

How do I make TestNG fail the build on skipped tests?

Use a listener that records configuration failures and fails the run, or configure the CI step to treat a non-zero skip count as a failure. The Surefire report shows skips separately, so a simple check on that count works well.

7Conclusion

In this answer, we saw TestNG's configuration annotations in order: @BeforeSuite once per suite, @BeforeTest once per <test> container of classes, @BeforeGroups before the first method of a group, @BeforeClass once per class instance, and @BeforeMethod before each method, with the @After versions in reverse. We saw that alwaysRun makes setup ignore group filters and makes clean-up run after failures, and that a failing @Before method turns dependent tests into SKIPPED results.

What to say in the interviewAbout 90 seconds aloud

Open with

TestNG follows its structure of suite, test, class and method. @BeforeSuite runs once for the whole run. @BeforeTest runs once per test element in testng.xml, and that is the confusing part for JUnit people: a test element is a container of classes, not one test method. @BeforeGroups runs once before the first method of the listed group is invoked, so only runs that include that group pay for its setup; because it fires right before that first method, in practice you can see it after that class's @BeforeClass. @BeforeClass runs once per instance of the test class, before its first method, and @BeforeMethod runs before every test method, which is where fresh state belongs.

Then, if they want more

The after annotations mirror this in reverse: after method, after groups, after class, after test, after suite. alwaysRun equals true on a before method makes it run whatever groups are selected, and on an after method it runs even if earlier methods failed, which is what clean-up needs.

If a before method throws, TestNG reports it as a failed configuration and the dependent tests as skipped, not failed. A failing @BeforeClass skips every method in that class and its @AfterClass unless alwaysRun is set. So I always check skip counts in CI, because the summary can show zero failures.

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 "List the TestNG configuration annotations from @BeforeSuite to @AfterMethod in the order...": https://scrivio-interview-library.netlify.app/java/testng/3990acb80b/