---
title: TestNG Interview Questions | TestingBot
description: TestNG questions show up in almost every Java automation interview, usually
  next to Selenium or Appium. Interviewers want to know whether you can make a…
source_url:
  html: https://testingbot.com/resources/courses/selenium-interview-questions/testng-interview-questions
  md: https://testingbot.com/resources/courses/selenium-interview-questions/testng-interview-questions.md
---

Course

[Selenium & Playwright Interview Questions](https://testingbot.com/resources/courses/selenium-interview-questions)

Chapter 6 of 8

1. [1 Selenium Interview Questions](https://testingbot.com/resources/courses/selenium-interview-questions)
2. [2 Advanced Selenium Interview Questions](https://testingbot.com/resources/courses/selenium-interview-questions/advanced-selenium-interview-questions)
3. [3 Playwright Interview Questions](https://testingbot.com/resources/courses/selenium-interview-questions/playwright-interview-questions)
4. [4 Cypress Interview Questions](https://testingbot.com/resources/courses/selenium-interview-questions/cypress-interview-questions)
5. [5 Appium Interview Questions](https://testingbot.com/resources/courses/selenium-interview-questions/appium-interview-questions)
6. [6 TestNG Interview Questions](https://testingbot.com/resources/courses/selenium-interview-questions/testng-interview-questions)
7. [7 Scenario and Coding Questions](https://testingbot.com/resources/courses/selenium-interview-questions/scenario-and-coding-questions)
8. [8 Knowledge check](https://testingbot.com/resources/courses/selenium-interview-questions/knowledge-check)

[Exit course](https://testingbot.com/resources/courses)

 Chapter 6 of 8 Intermediate 6 min read 2 quick checks Updated Nov 2026 

# TestNG Interview Questions
On this page
1. [Junior level](https://testingbot.com#junior-level)
2. [Mid level](https://testingbot.com#mid-level)
3. [Senior level](https://testingbot.com#senior-level)

TestNG questions show up in almost every Java automation interview, usually next to Selenium or Appium. Interviewers want to know whether you can make a suite run in parallel without the tests interfering with each other. Examples target TestNG 7.12, which requires Java 11 or later.

## Junior level[#](https://testingbot.com#junior-level)

### In what order do TestNG configuration methods run?[#](https://testingbot.com#in-what-order-do-testng-configuration-methods-run)

```
@BeforeSuite
  @BeforeTest <- once per <test> tag in testng.xml
    @BeforeClass
      @BeforeMethod
        @Test
      @AfterMethod
    @AfterClass
  @AfterTest
@AfterSuite
```

`@BeforeGroups` and `@AfterGroups` run before the first and after the last test method of a group.

- **Follow-up:**"Where do you create the WebDriver?" In `@BeforeMethod` for full isolation (a fresh browser per test), or `@BeforeClass` when a class's tests deliberately share one session.
- **Red flag:** Assuming `@BeforeTest` runs before every test method. It runs once per `<test>` element in `testng.xml`, which can contain many classes. This confusion is behind a lot of "the browser was already closed" bugs.

A class creates the driver in `@BeforeTest` and quits it in `@AfterMethod`. The first test method passes; every later one fails with "invalid session id". What do you change?

 Move the quit to `@AfterSuite` Add a retry analyzer to the later tests Create the driver in `@BeforeMethod`, so each test gets a fresh session that `@AfterMethod` quits

`@BeforeTest` runs once per `<test>` tag, but `@AfterMethod` runs after every method, so the second method uses a quit session. Pairing setup and teardown at the same level fixes it; retries would fail the same way.

### Hard assert or soft assert?[#](https://testingbot.com#hard-assert-or-soft-assert)

`Assert.assertEquals()` stops the test on the first failure. `SoftAssert` collects failures and reports them all when you call `assertAll()`:

```java
SoftAssert softly = new SoftAssert();
softly.assertEquals(page.title(), "Order confirmed");
softly.assertTrue(page.hasInvoiceLink(), "invoice link");
softly.assertAll(); // forget this and the test always passes
```

Soft asserts suit checking several independent fields on one page. Use hard asserts when later steps make no sense after a failure.

### How do you group and select tests?[#](https://testingbot.com#how-do-you-group-and-select-tests)

```java
@Test(groups = {"smoke", "checkout"})
public void guestCanCheckOut() { ... }
```

```xml
<test name="Smoke">
  <groups><run><include name="smoke"/></run></groups>
  <packages><package name="com.example.tests"/></packages>
</test>
```

With Maven Surefire you can also select groups on the command line with `-Dgroups=smoke`.

## Mid level[#](https://testingbot.com#mid-level)

### How do you run TestNG tests in parallel?[#](https://testingbot.com#how-do-you-run-testng-tests-in-parallel)

Set `parallel` and `thread-count` on the suite (or on a `<test>`):

```xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Regression" parallel="methods" thread-count="8">
  <test name="Chrome">
    <classes>
      <class name="com.example.tests.CheckoutTest"/>
      <class name="com.example.tests.SearchTest"/>
    </classes>
  </test>
</suite>
```

`parallel` accepts `methods`, `classes`, `tests` and `instances`. `tests` is a common way to run the same classes on several browsers at once, with one `<test>` per browser and a `<parameter name="browser" value="firefox"/>` read via `@Parameters("browser")`.

- **Follow-up:**"Now tests close each other's browsers. Why?" The driver is stored in a shared field. With `parallel="methods"` every method runs on its own thread, so the driver must be held in a `ThreadLocal<WebDriver>` (see the pattern in the [advanced Selenium chapter](https://testingbot.com/resources/courses/selenium-interview-questions/advanced-selenium-interview-questions)) or created per method.
- **Red flag:** A `static WebDriver driver` in a base class combined with `parallel="methods"`.

### What is a DataProvider, and can it run in parallel?[#](https://testingbot.com#what-is-a-dataprovider-and-can-it-run-in-parallel)

A method that supplies rows of arguments to a test:

```java
@DataProvider(name = "logins", parallel = true)
public Object[][] logins() {
    return new Object[][] {
        {"standard_user", true},
        {"locked_user", false},
    };
}

@Test(dataProvider = "logins")
public void login(String user, boolean shouldSucceed) { ... }
```

With `parallel = true` the rows run on separate threads; the pool size is the suite's `data-provider-thread-count` (default 10), not `thread-count`.

A parallel DataProvider supplies 40 rows and the suite sets `thread-count="20"`, yet only 10 rows run at a time. Why?

 Parallel DataProvider rows use `data-provider-thread-count`, which defaults to 10 The grid only has 10 free slots `parallel = true` is capped at 10 threads in TestNG 7

DataProvider parallelism has its own pool, set on the suite with `data-provider-thread-count`; `thread-count` does not affect it. Raise that attribute (and make sure your grid has the capacity).

### How do you take a screenshot on failure?[#](https://testingbot.com#how-do-you-take-a-screenshot-on-failure)

Implement `ITestListener` (its methods have default implementations, so override only what you need):

```java
public class ScreenshotListener implements ITestListener {
    @Override
    public void onTestFailure(ITestResult result) {
        WebDriver driver = DriverFactory.get();
        if (driver != null) {
            byte[] png = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
            // attach to your report, named after result.getName()
        }
    }
}
```

Register it with `@Listeners(ScreenshotListener.class)` on a class or a `<listeners>` block in `testng.xml`. See [what TestNG listeners are](https://testingbot.com/software-testing-questions/what-are-testng-listeners) for the other listener interfaces.

## Senior level[#](https://testingbot.com#senior-level)

### How do you retry failed tests, and should you?[#](https://testingbot.com#how-do-you-retry-failed-tests-and-should-you)

Implement `IRetryAnalyzer`:

```java
public class RetryOnce implements IRetryAnalyzer {
    private int attempts = 0;

    @Override
    public boolean retry(ITestResult result) {
        return attempts++ < 1;
    }
}

@Test(retryAnalyzer = RetryOnce.class)
public void flakyCheckout() { ... }
```

To apply it to every test without touching annotations, set it from an `IAnnotationTransformer` listener.

- **What the interviewer wants to hear:** retries are a reporting tool. Track which tests needed a retry and treat that list as a backlog; otherwise retries hide real defects, including race conditions in the product.

### `dependsOnMethods` and `priority`: when would you use them?[#](https://testingbot.com#dependsonmethods-and-priority-when-would-you-use-them)

`dependsOnMethods` skips a test when the method it depends on fails, and `priority` orders tests within a class. Both are legitimate, and both create coupling: a dependent test cannot run alone, and it cannot be spread across threads freely. A senior answer prefers independent tests that set up their own state through APIs, and uses dependencies only for genuinely sequential flows such as a long multi-step migration test.

### TestNG or JUnit 5?[#](https://testingbot.com#testng-or-junit-5)

Both are capable. TestNG's strengths are suite-level XML configuration, built-in parallel modes, groups, dependencies and parallel data providers, which is why many Selenium frameworks use it. JUnit 5 has the larger general Java ecosystem, extensions, and parallel execution configured in `junit-platform.properties`. Pick by team familiarity and existing tooling, and say so.

### How does a TestNG suite talk to a remote grid?[#](https://testingbot.com#how-does-a-testng-suite-talk-to-a-remote-grid)

Nothing in TestNG changes: the `@BeforeMethod` creates a `RemoteWebDriver` pointing at the grid URL, with the browser chosen from a `@Parameters` value. The [Selenium with TestNG course](https://testingbot.com/resources/courses/selenium-testng) builds this up step by step, and the [TestNG documentation for TestingBot](https://testingbot.com/support/web-automate/selenium/java/testng) shows a parallel suite on a cloud grid.

[Previous · Chapter 5 Appium Interview Questions](https://testingbot.com/resources/courses/selenium-interview-questions/appium-interview-questions) [Next · Chapter 7 Scenario and Coding Questions](https://testingbot.com/resources/courses/selenium-interview-questions/scenario-and-coding-questions)

On this page

1. [Junior level](https://testingbot.com#junior-level)
2. [Mid level](https://testingbot.com#mid-level)
3. [Senior level](https://testingbot.com#senior-level)
