TestNG
31 questions in 9 subtopics.
TestNG annotations and lifecycle
List the TestNG configuration annotations from@BeforeSuite to @AfterMethod in the order they run, and say at what scope each one runs.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);
}
}
AbstractTestNGSpringContextTests. Explain how Spring context loading interacts with the TestNG lifecycle and how you keep a 2,000 test suite fast.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?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
Write a TestNG data provider that feeds three pairs of (input, expected) into a test of anisValidEmail method, and explain how failures are reported.What is a TestNG @Factory, and when would you use it instead of a @DataProvider?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?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
ExplaindependsOnMethods and dependsOnGroups in TestNG. What happens to a dependent test when the test it depends on fails?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.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
In TestNG, write a test that expects anIllegalArgumentException with a message containing 'amount' from transfer(-5), and a test that fails if loadConfig() takes more than 2 seconds.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.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
What do parallel = "methods", "classes", "tests" and "instances" mean intestng.xml, and what is thread-count?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.CI runs a 4-hour TestNG suite. Leadership wants feedback in 20 minutes. Outline your plan.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
Mark some TestNG tests as 'smoke' and others as 'slow', and show how to run only smoke tests fromtestng.xml and from Maven Surefire.Define a group taxonomy for a TestNG suite used by 10 teams so that the CI pipeline can select tests reliably.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
Pass a base URL into a TestNG test fromtestng.xml, with a default when it is not set, and override it from the command line.Your team keeps 25 testng.xml files for different environments and test selections, and they drift. How would you simplify suite configuration?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
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?Tests pass in IntelliJ but in the Maven build some TestNG classes are not run at all. What do you check?Design how test results from TestNG and JUnit suites across 100 repositories are collected and used to improve quality.TestNG vs JUnit
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?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?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.No question matches these choices.