Notes
Lifecycle
Both have setup and teardown at several levels. Before method and before each run around every test. Before class and before all run once per class. TestNG adds before suite and before test, tied to its XML suite file; JUnit five does that with extensions or suite classes. A key difference: TestNG creates one instance of the test class for all its methods, while JUnit five creates a new instance per test, which makes shared fields safer in JUnit.
TestNG JUnit 5 @BeforeSuite (extension) @BeforeTest (suite level) @BeforeClass @BeforeAll @BeforeMethod @BeforeEach @AfterMethod @AfterEach @Test @Test TestNG: one instance per class JUnit 5: new instance per test
Data-driven
Data driven tests look different but do the same job. TestNG uses a data provider method that returns rows of objects, and the test names it. JUnit five uses parameterized tests with sources: CSV source for inline rows, method source for a method that streams arguments, enum and value sources too. Here both check ShopRung's quantity limits: zero is four hundred, one is two hundred, eleven is four hundred.
// TestNG
@DataProvider Object[][] qty() {
return new Object[][] {
{0, 400}, {1, 200}, {11, 400}}; }
@Test(dataProvider = "qty")
void limits(int q, int status) {..}
// JUnit 5
@ParameterizedTest
@CsvSource({"0,400","1,200","11,400"})
void limits(int q, int status) {..}
// also @MethodSource, @EnumSource Suites
TestNG suites are usually described in an XML file: a suite with parallel settings, tests inside it, groups to include or exclude, and the classes or packages to run. Many legacy frameworks are driven entirely by testng dot xml files, one per environment or test type. JUnit five uses annotated suite classes and Maven configuration instead, which keeps everything in code.
<suite name="ShopRung"
parallel="methods"
thread-count="4">
<test name="api">
<groups><run>
<include name="smoke"/>
</run></groups>
<packages><package
name="dev.algorung.api"/>
</packages></test>
</suite> Assertions
Sometimes you want every check reported, not just the first failure. TestNG has soft assert: collect failures, then call assert all at the end; forget that call and the test silently passes, a classic bug. JUnit five has assert all with lambdas, which runs every check and reports all failures together. Watch the argument order too: TestNG's assert equals takes actual first, JUnit takes expected first.
// TestNG SoftAssert soft = new SoftAssert(); soft.assertEquals(cart.discount(), 500); soft.assertEquals(cart.total(), 4999); soft.assertAll(); // fails here // JUnit 5 assertAll( () -> assertEquals(500, ...), () -> assertEquals(4999, ...));
Listeners and extensions
Both let you plug in code around tests. TestNG uses listeners, such as an I test listener with on test failure, registered with an annotation or in the XML. JUnit five uses extensions: a test watcher with test failed, or an after test execution callback as in the Selenium lab, registered with extend with or register extension. Typical uses: screenshots, logs, timing, retries and reporting.
// TestNG
class Shots implements ITestListener {
public void onTestFailure(
ITestResult r) { save(...); } }
@Listeners(Shots.class)
// JUnit 5
class Shots implements
TestWatcher { testFailed(...) }
@ExtendWith(Shots.class)
// or AfterTestExecutionCallback Order and dependencies
TestNG supports dependencies between tests: cancel order depends on place order, and is skipped if it fails. JUnit five only offers an order annotation. Use these rarely. Tests that depend on each other cannot run alone or in parallel, as the flaky test lab showed. It is almost always better for each test to create what it needs, such as its own order through the API.
// TestNG
@Test void placeOrder() {...}
@Test(dependsOnMethods =
"placeOrder")
void cancelOrder() {...}
// skipped if placeOrder fails
// JUnit 5: @Order + @TestMethodOrder
// better in both: each test makes
// its own order (flaky-test lab) How to choose
How to choose. JUnit five is the default for new Java projects and is what Spring and modern tools expect, with extensions and nested tests. TestNG is in many existing Selenium frameworks, with XML suites, dependencies and rich listeners. Both run in parallel, both are data driven, both have groups or tags, Allure reports and CI support. In practice: learn both, and follow the codebase you join.
- JUnit 5: default for new Java projects, Spring, Cucumber; extensions, @Nested, modern
- TestNG: many existing Selenium frameworks; XML suites, dependencies, rich listeners
- both: parallel, data-driven, groups/tags, Allure, CI → learn both; follow the repo
Common mistakes
Runner mistakes. Soft assert without assert all, so failures vanish. Actual and expected swapped, so messages mislead. A static web driver in TestNG parallel runs, where one instance serves many methods. Long chains of depends on methods. Mixing JUnit four, JUnit five and TestNG imports in one class, where some annotations are silently ignored. And XML suite files that nobody updates, so new tests never run.
- SoftAssert without assertAll()
- actual/expected swapped
- static WebDriver in TestNG parallel runs
- long dependsOnMethods chains
- mixing JUnit 4, JUnit 5 and TestNG imports in one class
- testng.xml nobody updates
Say it like this
In the interview: both are good. TestNG offers XML suites, groups, data providers, dependencies between tests and rich listeners, and is common in older Selenium frameworks. JUnit five creates a new instance per test and offers parameterized sources, tags, extensions and assert all, and is the default for new projects. I avoid test dependencies in both, keep tests independent for parallel runs, and follow what the repository already uses.
- "Both are good. TestNG: XML
- suites, groups, data providers, dependencies, listeners; common
- in older Selenium frameworks.
- JUnit 5: new instance per test, parameterized sources, tags, extensions, assertAll; default
- for new projects. I avoid test
- dependencies in both and follow what the repo uses."