skip to content

Why might a Java team choose TestNG over JUnit Jupiter for a test suite, and when is JUnit Jupiter the better default?

level: juniorimportance: must knowfreq 45%

answer

  1. suite-level control vs ecosystem default
  2. one suite file drives the run
  3. a test can depend on another
  4. hooks per suite and per test block
  5. what the stack already assumes

basics

~20 s

Teams pick TestNG for suite-level control: a testng.xml suite file, test-to-test dependencies, data providers, and suite and test-block hooks built in. JUnit Jupiter is the better default where the stack, frameworks and team already assume it.

solid answer

~40 s

The two overlap on most features: annotations, setup and teardown hooks, parameterised tests, tagging and parallel runs. TestNG earns its place on large, ordered UI or API suites: the `testng.xml` suite file decides what runs, with which parameters and how parallel; `@Test(dependsOnMethods = ...)` skips a test whose prerequisite failed; and hooks exist at suite and `<test>`-block scope as well as class and method. JUnit Jupiter is the better default for most new application code, because many JVM frameworks' test support, tutorials and teammates assume it, and its extension model is the common integration point. JUnit 6 also needs Java 17, while TestNG 7.12 still needs only Java 11. So I justify TestNG by naming the features the suite actually uses, not by habit.

go deeper

for a junior

Recall three TestNG differentiators: the testng.xml suite file, dependsOnMethods with dependents reported as skipped, and hooks at suite and test-block scope. Then say why JUnit Jupiter is the common default.

for a middle

Explain what each differentiator buys in practice, such as one suite file per CI job and clean skips after a broken login, and note the default instance difference between the two frameworks.

for a senior

Tie the choice to the workload and the inherited codebase, and say where a feature becomes a liability, for example dependencies blocking isolated reruns or parallel scheduling.

for a principal

Frame it as a portfolio decision: which suites justify TestNG's suite model, what a second runner costs the organisation, and which rule decides new code.

## What the two frameworks share **TestNG** and **JUnit Jupiter** (the programming model of JUnit 5 and JUnit 6) are both annotation-driven test runners for the JVM. For everyday unit testing they look almost the same: a method marked `@Test`, setup and teardown hooks, assertions, a way to run one method with many argument rows, a way to label tests and run only a subset, and opt-in parallel execution. That overlap is why the choice rarely turns on whether a framework *can* do something. It turns on how much of the run each framework puts under the suite author's control, and on what the codebase and its tools already expect. ## Where TestNG differs TestNG was designed to cover integration and end-to-end tests as well as unit tests, which is why it is common under Selenium and HTTP API suites: - **A suite file drives the run.** `testng.xml` lists suites, `<test>` blocks, classes, methods, included and excluded groups, parameter values and the parallel setting in one declarative file. Changing what a CI job runs means editing XML, not code. - **Tests can depend on tests.** `@Test(dependsOnMethods = ...)` and `dependsOnGroups` let a test run only after another passed; when the prerequisite fails, the dependent is reported as **skipped**, not failed, so one broken login does not produce fifty misleading failures. - **Hooks at more scopes.** Besides class and method hooks, TestNG has `@BeforeSuite`, `@BeforeTest` (around a `<test>` block of the suite file) and `@BeforeGroups`. - **Data and instances.** `@DataProvider` feeds argument rows and can itself run rows in parallel; `@Factory` creates several instances of a test class. - **One shared instance per class** by default, which suits a scenario that walks a flow across several methods (and is a trap when tests are meant to be independent). - **Retry and listener hooks** such as `IRetryAnalyzer` and `ITestListener` are part of TestNG's core API. ## Where JUnit Jupiter is the stronger default - **Ecosystem gravity.** Many application frameworks' test support, build-tool defaults, examples and new hires assume JUnit Jupiter. - **Extension model.** One extension API is how most libraries plug into JUnit; it covers what TestNG splits across listeners and hooks. - **A platform, not only a runner.** JUnit's platform can host several test engines in one launch, which matters when a codebase mixes styles. - **Independence by default.** Jupiter creates a fresh instance per test method by default and offers no test-to-test dependency annotation, which nudges teams toward independent tests. - **Language baseline.** JUnit 6 requires Java 17 at runtime; TestNG has required Java 11 since 7.6.0, so a codebase stuck below 17 cannot move to JUnit 6. ## Side by side | Concern | TestNG | JUnit Jupiter | |---|---|---| | What runs | `testng.xml` suite file, or classes and groups | Class discovery plus tag filters; a `@Suite` class for grouping | | Test dependencies | `dependsOnMethods`, `dependsOnGroups` (dependent skipped) | None; method ordering exists but does not gate execution | | Labels | `groups` | `@Tag` | | Argument rows | `@DataProvider` | `@ParameterizedTest` with a source | | Hook scopes | suite, `<test>` block, groups, class, method | class and method; suite hooks via the separate suite engine | | Default instance | one per class, shared by its methods | a new one per test method | | Minimum Java | 11 (TestNG 7.6 onward) | 17 (JUnit 6) | ## How to justify the choice in an interview 1. **Name the workload.** A long, ordered Selenium regression pack with environment parameters per CI job leans TestNG; unit and slice tests inside a service lean JUnit Jupiter. 2. **Name the features actually used.** If nobody uses suite files, dependencies or data-provider parallelism, TestNG's advantages are theoretical. 3. **Weigh the inherited codebase.** The cheapest framework is usually the one thousands of existing tests already use; migration cost is real. 4. **Mention the trade-off honestly.** Test dependencies make a suite faster to triage but harder to run in isolation; independence costs setup time. A weak answer claims one framework "can't do" parameterised tests, parallelism or tagging. Both can; the difference is in the suite-level model, the dependency feature and the ecosystem.

  • Is TestNG's support for test dependencies an argument for it or against it?
    Both. In a UI or API suite, `dependsOnMethods` turns one broken prerequisite into a clean set of skips, which speeds triage. The cost is coupling: a dependent test cannot be run alone without its prerequisite, ordering constraints limit parallelism, and the habit can spread to unit tests that should be independent. A lead often allows it only in end-to-end flows.
  • Does choosing TestNG mean giving up parameterised or parallel tests that JUnit Jupiter has?
    No. TestNG has `@DataProvider` for argument rows and a `parallel` attribute in `testng.xml` with several modes. JUnit Jupiter has `@ParameterizedTest` and opt-in parallel execution through configuration parameters. The frameworks differ in where these are configured and in defaults, not in whether the feature exists.

  • JUnit Jupiter cannot run parameterised or data-driven tests at all
  • TestNG cannot run tests in parallel without a third-party plugin
  • JUnit Jupiter has a dependsOnMethods equivalent that skips dependents
  • JUnit Jupiter has no way to label tests and run only a subset
  • The choice is pure preference with no technical difference worth naming