TestingTestingbeginner

TestNG Interview Questions for Freshers

25 TestNG questions for Java automation testing roles: annotation order, priority, groups, dependencies, parameters from testng.xml, DataProvider, hard vs soft assertions, listeners and parallel runs, with real output.

Published
TL;DR – Quick Answer

TestNG fresher interviews focus on annotations and the order they run in, priority and dependsOnMethods, groups, testng.xml, parameters and DataProvider, and the difference between hard and soft assertions. Prepare a short answer for each and be able to write a small test class with setup, two tests and a DataProvider.

On This Page

TestNG interview questions for freshers check whether you can organise and run Java tests. Expect questions on annotations and their order, priority, groups, dependencies, testng.xml, DataProvider and assertions. This page has 25 questions with short answers. The code was run with TestNG 7.10 on Java 17, and the output shown is the real output.

Snippets show only the useful part of a class. Imports come from org.testng.annotations.* and org.testng.Assert unless shown.

How to answer TestNG interview questions

Answer with the annotation or attribute, what it does and where you used it. "@BeforeMethod runs before every test method. I used it to open a fresh browser so each test starts clean."

Pro tip: Interviewers often ask for the order of TestNG annotations. Learn it as Suite, Test, Class, Method, going in for Before and coming out for After.

TestNG basics questions

Q1. What is TestNG?

30-second answer: TestNG is a testing framework for Java, inspired by JUnit, whose name stands for "Test Next Generation". It runs tests, handles setup and teardown, supports groups, dependencies, parameters, data-driven tests and parallel runs, and creates HTML reports. It is widely used with Selenium.

Q2. What are the advantages of TestNG over JUnit 4?

30-second answer: TestNG added several features earlier than JUnit 4: suite, test and group-level setup annotations, test dependencies, built-in DataProvider, groups and parallel runs configured in XML. JUnit 5 has closed much of this gap, so in an interview say which one your project used and why.

Q3. How do you add TestNG to a Maven project?

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>7.10.2</version>
  <scope>test</scope>
</dependency>

30-second answer: Add the dependency with test scope and run tests with mvn test. To run a testng.xml file through Maven, configure it in the Surefire plugin's suiteXmlFiles.

Q4. What is the role of the @Test annotation?

30-second answer: @Test marks a method as a test. TestNG runs it, records pass, fail or skip, and reports it. Attributes such as priority, groups, dependsOnMethods, enabled, timeOut, invocationCount, dataProvider and expectedExceptions change how it runs.

Annotation questions

Q5. What are the main TestNG annotations?

Annotation Runs
@BeforeSuite / @AfterSuite Once before / after all tests in the suite
@BeforeTest / @AfterTest Before / after each <test> tag in testng.xml
@BeforeClass / @AfterClass Once before / after the test methods of a class
@BeforeMethod / @AfterMethod Before / after every test method
@BeforeGroups / @AfterGroups Before / after the first and last test of a group
@Test Marks a test
@DataProvider Supplies data rows to a test
@Parameters Reads values from testng.xml

30-second answer: Before and After annotations work at four levels: suite, test, class and method. Use the method level for things each test needs fresh, such as a browser, and the suite level for one-time setup, such as reading config.

Q6. In what order do TestNG annotations run?

public class LifecycleTest {
    @BeforeSuite  public void beforeSuite()  { System.out.println("BeforeSuite"); }
    @BeforeTest   public void beforeTest()   { System.out.println("BeforeTest"); }
    @BeforeClass  public void beforeClass()  { System.out.println("BeforeClass"); }
    @BeforeMethod public void beforeMethod() { System.out.println("BeforeMethod"); }

    @Test public void testA() { System.out.println("  testA"); }
    @Test public void testB() { System.out.println("  testB"); }

    @AfterMethod  public void afterMethod()  { System.out.println("AfterMethod"); }
    @AfterClass   public void afterClass()   { System.out.println("AfterClass"); }
    @AfterTest    public void afterTest()    { System.out.println("AfterTest"); }
    @AfterSuite   public void afterSuite()   { System.out.println("AfterSuite"); }
}

Output:

BeforeSuite
BeforeTest
BeforeClass
BeforeMethod
  testA
AfterMethod
BeforeMethod
  testB
AfterMethod
AfterClass
AfterTest
AfterSuite

30-second answer: Suite, Test, Class, Method on the way in, then the reverse on the way out. @BeforeMethod and @AfterMethod run around every test.

Q7. What is the difference between @BeforeTest and @BeforeMethod?

30-second answer: @BeforeTest runs once before each <test> block in testng.xml, which can contain many classes. @BeforeMethod runs before every single @Test method. The names confuse many candidates, so say this clearly.

Q8. In what order do test methods run without priority?

30-second answer: By default TestNG sorts test methods by name, so do not rely on the order in which they appear in the file. If order matters, use priority or dependsOnMethods, or better, make tests independent.

Priority, dependency and control questions

Q9. How does priority work?

public class PriorityTest {
    @Test(priority = 2) public void checkout() { System.out.println("checkout"); }
    @Test(priority = 1) public void addToCart() { System.out.println("addToCart"); }
    @Test public void login() { System.out.println("login"); }
}

Output:

login
addToCart
checkout

30-second answer: Lower priority runs first. The default is 0, so login, which has no priority, runs before priority 1 and 2. Priorities can be negative.

Q10. What does dependsOnMethods do?

@Test
public void login() {
    Assert.assertTrue(false, "login failed");
}

@Test(dependsOnMethods = "login")
public void viewProfile() {
    System.out.println("viewProfile");
}

30-second answer: viewProfile runs only if login passes. Here login fails, so TestNG marks viewProfile as skipped instead of failed, and "viewProfile" is never printed. The run reported 1 failure and 1 skip. This keeps one root failure from showing up as many failures.

Q11. How do you skip or disable a test?

30-second answer: @Test(enabled = false) disables a test so it does not run. Inside a test, throw new SkipException("reason") skips it at run time, for example when a feature is turned off in that environment.

Q12. What do timeOut, invocationCount and expectedExceptions do?

Attribute Example Meaning
timeOut @Test(timeOut = 2000) Fail if the test takes more than 2000 ms
invocationCount @Test(invocationCount = 3) Run the test 3 times
expectedExceptions @Test(expectedExceptions = ArithmeticException.class) Pass only if that exception is thrown

30-second answer: These control how a single test runs without extra code inside it.

Groups and testng.xml questions

Q13. What are groups in TestNG?

30-second answer: Groups tag tests so you can run a subset, such as only smoke tests.

@Test(groups = {"smoke", "regression"})
public void loginWorks() { }

@Test(groups = "regression")
public void editProfile() { }

In testng.xml, include or exclude groups:

<groups>
  <run>
    <include name="smoke"/>
  </run>
</groups>

Q14. What is testng.xml?

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Regression">
  <test name="Login tests">
    <classes>
      <class name="tests.LoginTest"/>
      <class name="tests.LogoutTest"/>
    </classes>
  </test>
</suite>

30-second answer: testng.xml is a configuration file that defines a suite. A suite contains one or more <test> blocks, and each block lists classes, packages, groups or methods. It is also where you set parameters and parallel runs.

Q15. How do you pass parameters from testng.xml?

<test name="Params">
  <parameter name="browser" value="chrome"/>
  <parameter name="env" value="staging"/>
  <classes><class name="demo.ParamTest"/></classes>
</test>
@Test
@Parameters({"browser", "env"})
public void open(String browser, String env) {
    System.out.println("Opening " + browser + " on " + env);
}

Output:

Opening chrome on staging

30-second answer: Declare <parameter> tags in the XML and read them with @Parameters. This is how one suite runs on different browsers or environments without code changes.

Q16. How do you run tests in parallel?

30-second answer: Set parallel and thread-count on the suite: <suite name="Regression" parallel="methods" thread-count="4">. parallel can be methods, classes, tests or instances. For Selenium, each thread needs its own WebDriver, usually kept in a ThreadLocal.

Data-driven testing questions

Q17. What is a DataProvider?

@DataProvider(name = "sums")
public Object[][] sums() {
    return new Object[][] { {2, 3, 5}, {10, -4, 6}, {0, 0, 0} };
}

@Test(dataProvider = "sums")
public void add(int a, int b, int expected) {
    System.out.println(a + " + " + b + " = " + (a + b));
    Assert.assertEquals(a + b, expected);
}

Output:

2 + 3 = 5
10 + -4 = 6
0 + 0 = 0

30-second answer: A DataProvider returns rows of data, and the test runs once per row. Each run is reported as a separate result. Common uses are login with many credentials and forms with many inputs.

Q18. What is the difference between @Parameters and @DataProvider?

Point @Parameters @DataProvider
Data comes from testng.xml A Java method
Runs the test Once with those values Once per data row
Best for Browser, environment, URL Many input combinations

30-second answer: Parameters set the run's configuration; DataProvider feeds test data.

Q19. Can a DataProvider be in another class?

30-second answer: Yes. Use @Test(dataProvider = "logins", dataProviderClass = TestData.class). Make the DataProvider method static when it lives in another class, so TestNG can call it without creating that class. Data providers can also read rows from Excel, CSV or JSON files.

Assertion questions

Q20. What is the difference between hard and soft assertions?

import org.testng.asserts.SoftAssert;

@Test
public void checkProfile() {
    SoftAssert soft = new SoftAssert();
    soft.assertEquals("Asha", "Asha", "name");
    soft.assertEquals("Pune", "Hyderabad", "city");
    soft.assertTrue(false, "photo shown");
    System.out.println("all checks ran");
    soft.assertAll();
}

The test prints all checks ran and then fails with:

The following asserts failed:
	city expected [Hyderabad] but found [Pune],
	photo shown expected [true] but found [false]

30-second answer: A hard assertion (Assert) stops the test at the first failure. A soft assertion (SoftAssert) records failures and keeps going, then reports them all at assertAll(). Forgetting assertAll() is a common mistake: the test passes even when checks fail.

Q21. Which assertion methods do you use most?

30-second answer: assertEquals(actual, expected), assertNotEquals, assertTrue, assertFalse, assertNull, assertNotNull and fail. In TestNG the order is actual first, then expected, which is the reverse of JUnit. Add a message as the last argument so failures are easy to read.

Listener, reporting and retry questions

Q22. What are TestNG listeners?

30-second answer: Listeners are classes that react to test events. ITestListener has methods such as onTestStart, onTestSuccess and onTestFailure. A common use is taking a screenshot in onTestFailure. Register a listener with @Listeners(MyListener.class) on a class or a <listeners> tag in testng.xml.

Q23. How do you re-run failed tests?

30-second answer: Two ways. TestNG writes a testng-failed.xml file in the output folder after a run, which you can run to repeat only the failed tests. Or implement IRetryAnalyzer and set @Test(retryAnalyzer = Retry.class) to retry a failing test a set number of times. Retries hide flaky tests, so fix the cause too.

Q24. What reports does TestNG create?

30-second answer: TestNG creates index.html and emailable-report.html, plus JUnit-style XML that CI tools read. When run through Maven, these are in target/surefire-reports. Many teams add Extent Reports or Allure for nicer reports with screenshots.

Q25. How does TestNG fit into a Selenium framework?

30-second answer: A base test class opens the browser in @BeforeMethod and quits it in @AfterMethod. Page classes hold locators and actions. Tests use @Test with groups such as smoke and regression, DataProviders feed data, a listener captures screenshots on failure, and testng.xml decides what runs in CI. The Selenium interview questions page covers the Selenium side.

Quick revision table

Topic One line to remember
Annotation order Suite, Test, Class, Method, then reverse
priority Lower runs first, default 0
No priority Methods run in name order
dependsOnMethods Skips if the dependency fails
Groups Tag and filter tests
@Parameters Values from testng.xml
@DataProvider One run per data row
SoftAssert Collects failures, needs assertAll()
Retry testng-failed.xml or IRetryAnalyzer

How to prepare in one week

Day Task
1 Maven project, first @Test, assertions
2 All Before and After annotations, print the order
3 Priority, dependencies, enabled, timeOut
4 Groups and testng.xml
5 Parameters and DataProvider
6 Listener with screenshot, parallel run with Selenium
7 Revise and do a mock interview

TestNG questions usually sit inside an automation round, so revise the automation testing interview questions for freshers as well. If your target project uses BDD, continue with the Cucumber and BDD interview questions.

Frequently Asked Questions

Should I learn TestNG or JUnit for automation testing?
Many Selenium with Java projects in Indian service companies use TestNG, so it appears often in job posts. JUnit 5 is also common, especially in developer teams. Learn the one in your target job posts; the ideas of annotations, assertions and setup carry over.
Do I need to know Selenium before learning TestNG?
No. TestNG is a test runner and works with any Java code. Learn it with simple tests first, then use it to run Selenium tests. Interviewers usually ask about both together.
Which TestNG version should I use?
Use a recent 7.x release from Maven Central. Recent versions need Java 11 or later. Mention the version in your project if asked, and check the official TestNG site for current releases.
Is testng.xml required to run tests?
No. You can run a class directly from the IDE or through Maven. testng.xml is useful when you want to choose classes, include or exclude groups, pass parameters or run tests in parallel.
What TestNG project should I show in an interview?
A small Selenium or API test project with a base class for setup and teardown, a few tests in groups such as smoke and regression, one DataProvider, a testng.xml and a report. Put it on GitHub with a README that explains how to run it.
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 5 December 2026 LinkedIn
Chat with us