Scrivio Interview library

TestNG

31 questions in 9 subtopics.

TestNG annotations and lifecycle

ConceptFoundations7 min read
List the TestNG configuration annotations from @BeforeSuite to @AfterMethod in the order they run, and say at what scope each one runs.
DebuggingFoundations6 min read
In this TestNG class, the second test fails only when both tests run:
public class CartTest {
private Cart cart = new Cart();
@Test
public void addsItem() {
cart.add("a");
assertEquals(cart.size(), 1);
}
@Test
public void startsEmpty() {
assertEquals(cart.size(), 0);
}
}
Why, and how do you fix it?
DesignIntermediate7 min read
A TestNG suite for a Spring Boot application extends AbstractTestNGSpringContextTests. Explain how Spring context loading interacts with the TestNG lifecycle and how you keep a 2,000 test suite fast.
ScenarioAdvanced6 min read
Your organisation has a large legacy TestNG suite with deep base class hierarchies of @BeforeClass and @BeforeMethod hooks. Failures cascade into hundreds of SKIPPED tests. How would you improve it?
LeadershipPrincipal7 min read
Test frameworks in Java have changed several times (JUnit 3, TestNG, JUnit 4, JUnit 5 and 6). What lessons would you take for choosing long-lived developer tools?

Data providers and factories

CodingFoundations7 min read
Write a TestNG data provider that feeds three pairs of (input, expected) into a test of an isValidEmail method, and explain how failures are reported.
ConceptIntermediate5 min read
What is a TestNG @Factory, and when would you use it instead of a @DataProvider?
DesignAdvanced7 min read
Teams load test data for TestNG data providers from large spreadsheets maintained by QA. Tests are slow and failures are hard to read. How would you redesign data-driven testing?
DesignPrincipal8 min read
Data-driven testing frameworks (TestNG data providers, spreadsheet-driven suites, BDD tables) let non-developers define tests. When is that worth it for a large organisation?

Dependent tests

ConceptFoundations6 min read
Explain dependsOnMethods and dependsOnGroups in TestNG. What happens to a dependent test when the test it depends on fails?
ScenarioIntermediate7 min read
An end-to-end TestNG suite uses a chain of 40 dependent tests: create user, log in, create order, pay, and so on. Discuss the trade-offs and how you would restructure it.
DesignAdvanced7 min read
When would you allow test dependencies (TestNG dependsOnMethods or ordered tests) in a company test standard, and when would you forbid them?

Expected exceptions and timeouts

CodingFoundations6 min read
In TestNG, write a test that expects an IllegalArgumentException with a message containing 'amount' from transfer(-5), and a test that fails if loadConfig() takes more than 2 seconds.
DebuggingIntermediate6 min read
A TestNG test with @Test(timeOut = 5000) that uses a Spring @Transactional test context sees no rollback, and data from it remains in the database. Explain.
LeadershipPrincipal7 min read
Flaky timeouts in TestNG and JUnit suites cost your engineering organisation many hours per week in reruns. How would you lead a company-wide reduction of test flakiness?

Parallel test execution

ConceptFoundations6 min read
What do parallel = "methods", "classes", "tests" and "instances" mean in testng.xml, and what is thread-count?
DebuggingIntermediate6 min read
After switching a Selenium TestNG suite to parallel = "methods" with thread-count = 8, tests fail randomly with sessions closed or elements from the wrong page. Diagnose and fix.
DesignAdvanced7 min read
CI runs a 4-hour TestNG suite. Leadership wants feedback in 20 minutes. Outline your plan.
DesignPrincipal6 min read
Your company spends a large share of its CI budget on test execution. As a senior architect, how do you weigh more parallelism, better test design and test selection against each other?

Test groups and selection

CodingFoundations6 min read
Mark some TestNG tests as 'smoke' and others as 'slow', and show how to run only smoke tests from testng.xml and from Maven Surefire.
DesignIntermediate7 min read
Define a group taxonomy for a TestNG suite used by 10 teams so that the CI pipeline can select tests reliably.
LeadershipPrincipal6 min read
Test suites grew to the point where nobody can run all tests before a release. How do you decide, at company level, which tests run where, and who owns that decision?

Testng.xml parameters

CodingFoundations6 min read
Pass a base URL into a TestNG test from testng.xml, with a default when it is not set, and override it from the command line.
DesignIntermediate6 min read
Your team keeps 25 testng.xml files for different environments and test selections, and they drift. How would you simplify suite configuration?
ScenarioAdvanced6 min read
TestNG suites at your company read environment URLs and credentials from testng.xml files committed to Git. Security asks you to fix this across 60 repositories.

Running TestNG in builds and IDEs

ConceptFoundations6 min read
How do you run TestNG tests with Maven and with Gradle, and why do some teams see 'No tests were executed' after adding JUnit 5 to the same project?
DebuggingIntermediate7 min read
Tests pass in IntelliJ but in the Maven build some TestNG classes are not run at all. What do you check?
DesignAdvanced6 min read
Design how test results from TestNG and JUnit suites across 100 repositories are collected and used to improve quality.

TestNG vs JUnit

ConceptFoundations6 min read
Compare TestNG with JUnit Jupiter for a new Java 25 project. Which features of TestNG have JUnit Jupiter caught up on, and which remain different?
ScenarioIntermediate6 min read
You join a team with 6,000 TestNG tests and the platform team asks everyone to move to JUnit 5 or 6. How do you decide and plan?
LeadershipPrincipal6 min read
Should a company with 400 Java repositories mandate one test framework? Use TestNG and JUnit as the example and discuss what standards are worth enforcing.