skip to content

When mapping a TestNG suite onto JUnit Jupiter, what replaces groups, @DataProvider, @Factory and testng.xml, and what has no equivalent?

level: middleimportance: should knowfreq 40%

answer

  1. labels, rows, instances, suite
  2. tags and tag expressions
  3. parameterised test with a method source
  4. factory is not TestFactory
  5. ordering is not dependency

basics

~10 s

Groups become @Tag, @DataProvider becomes @ParameterizedTest with @MethodSource, @Factory is closest to the experimental @ParameterizedClass, and testng.xml becomes a @Suite class plus configuration parameters. dependsOnMethods and @BeforeTest have no direct equivalent.

solid answer

~40 s

TestNG `groups` map to JUnit Jupiter's `@Tag`, selected with tag expressions. `@DataProvider` maps to `@ParameterizedTest` with `@MethodSource` or another argument source. `@Factory`, which builds several instances of a test class, is closest to `@ParameterizedClass` (still experimental in JUnit 6.1), not to `@TestFactory`, which generates dynamic tests from one method. The `testng.xml` suite file has no XML counterpart: a `@Suite` class selects tests, and configuration parameters handle parallelism; suite-file `@Parameters` have no direct replacement. `dependsOnMethods` has none either: `@TestMethodOrder` orders methods but never skips one. Smaller ones map cleanly: `invocationCount` to `@RepeatedTest`, `timeOut` to `@Timeout`, `expectedExceptions` to `assertThrows`, `enabled = false` to `@Disabled`.

code

java · 17 lines
java
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

public class DiscountTest {

  @DataProvider(name = "prices")
  public Object[][] prices() {
    return new Object[][] {{100, 90}, {200, 180}};
  }

  @Test(groups = "smoke", dataProvider = "prices")
  public void appliesTenPercent(int gross, int net) {
    assertEquals(gross * 9 / 10, net);
  }
}

go deeper

for a junior

Recall the clean pairs: groups to @Tag, @DataProvider to @ParameterizedTest with @MethodSource, invocationCount to @RepeatedTest, enabled false to @Disabled.

for a middle

Explain the false friends, @Factory against @TestFactory and @BeforeTest against @BeforeEach, and say why dependsOnMethods and suite-file @Parameters have no direct Jupiter counterpart.

for a senior

Show that the no-equivalent rows drive migration cost, and describe how you redesign dependency chains and suite-file parameters instead of translating them.

for a principal

Use the mapping to estimate effort: mechanical rows are cheap, redesign rows are where a migration plan, its risk and its timeline are decided.

## Why a mapping table is only half the job When a team moves tests between **TestNG** and **JUnit Jupiter** (the JUnit 5 and JUnit 6 programming model), most annotations have a counterpart with a different name. The interview signal is knowing which pairs are **real equivalents**, which are **false friends** (same-sounding names that do something else), and which TestNG features have **no equivalent at all**, because those are where migration effort and risk concentrate. ## Clean equivalents | TestNG | JUnit Jupiter | Note | |---|---|---| | `@Test(groups = "smoke")` | `@Tag("smoke")` | JUnit's own guide calls tags analogous to TestNG groups | | `@DataProvider` + `@Test(dataProvider = ...)` | `@ParameterizedTest` + `@MethodSource` | other Jupiter sources: CSV, enum, values | | `@Test(invocationCount = 3)` | `@RepeatedTest(3)` | | | `@Test(timeOut = ...)` | `@Timeout` | | | `@Test(expectedExceptions = ...)` | `assertThrows(...)` | Jupiter scopes the expectation to one lambda | | `@Test(enabled = false)` | `@Disabled` | | | `@BeforeMethod` / `@AfterMethod` | `@BeforeEach` / `@AfterEach` | | | `@BeforeClass` / `@AfterClass` | `@BeforeAll` / `@AfterAll` | Jupiter's are `static` unless the class uses the per-class instance lifecycle | | `ITestListener`, `@Listeners` | the extension model, launcher listeners | different shape, same purpose | ## False friends These pairs share a word and catch people who map by name: - **`@Factory` vs `@TestFactory`.** TestNG's `@Factory` returns test-class *instances* (for example, one per browser or per tenant), and every `@Test` method runs on each. Jupiter's `@TestFactory` is a single method that returns *dynamic tests* built at run time. The nearer Jupiter counterpart to a factory fed by a data provider is `@ParameterizedClass`, which runs a whole class once per argument set; the JUnit 6.1 user guide still marks it **experimental**. - **`@BeforeTest` vs `@BeforeEach`.** TestNG's `@BeforeTest` runs once around a `<test>` block of `testng.xml`, not before each test method. Jupiter has no `<test>`-block scope, so mapping it to `@BeforeEach` changes how often it runs. - **`@Parameters` vs `@ParameterizedTest`.** TestNG's `@Parameters` injects named values from the suite file (an environment URL, a browser name). Jupiter's `@ParameterizedTest` is the data-driven feature, the counterpart of `@DataProvider`. ## No direct equivalent - **`dependsOnMethods` and `dependsOnGroups`.** TestNG skips a dependent test when its prerequisite fails. Jupiter's `@TestMethodOrder` with `@Order` only controls the order; a later method still runs, and fails on its own, when an earlier one fails. - **The suite file itself.** A `testng.xml` declares classes, included and excluded groups, parameters and the parallel mode in one XML file. In Jupiter those concerns are split: a `@Suite` class (from the separate suite engine) selects classes, packages and tags; configuration parameters enable parallel execution; values that `@Parameters` supplied typically come from configuration parameters, system properties or environment variables instead. - **Hooks at groups and `<test>` scope.** `@BeforeGroups` and `@BeforeTest` have no Jupiter annotation; suite-level hooks exist only in the suite engine. - **A built-in retry hook.** TestNG's `IRetryAnalyzer` is core API; Jupiter's core has no retry annotation, so teams use an extension. ## Using the map in practice 1. **Inventory first.** Count usages of each TestNG feature, because the clean equivalents are mechanical and the rest is design work. 2. **Translate the mechanical rows** (groups, providers, repetitions, timeouts, disabled tests) with care for argument order in assertions. 3. **Redesign the no-equivalent rows.** Dependency chains usually become one test with several steps, or independent tests with their own setup; suite-file parameters become configuration that the run supplies. 4. **Re-check the false friends by hand**, since they compile in their new form and behave differently. ## The reverse direction The map also runs from JUnit Jupiter to TestNG, and the gaps there are different: - **`@Nested` classes** have no TestNG counterpart; the hierarchy usually flattens into separate classes or groups. - **`@TestFactory` dynamic tests** have no direct TestNG equivalent; a data provider or a factory usually takes their place. - **Extensions** become TestNG listeners or hooks, and one extension may need several TestNG interfaces. - **The per-method instance** that Jupiter gave each test is gone: TestNG shares one instance across a class's methods, so field state that used to be fresh now carries over. Knowing both directions is what lets you answer the interview follow-up "and what would you lose going the other way?"

  • Why is mapping TestNG's @Factory to JUnit Jupiter's @TestFactory a mistake?
    They solve different problems. TestNG's `@Factory` returns several instances of a test class, and every `@Test` method runs on each instance, so a class runs once per browser or tenant. Jupiter's `@TestFactory` is one method returning dynamic tests created at run time. The closer Jupiter feature is `@ParameterizedClass`, which JUnit 6.1 still marks experimental.
  • What do you do with a TestNG dependsOnMethods chain when moving to JUnit Jupiter?
    There is no annotation to translate it to. Usually the chain becomes one test whose steps run in sequence, or independent tests that each build their own precondition. Ordering with `@TestMethodOrder` keeps the sequence but not the skip, so a broken first step produces a run of confusing failures instead of skips.

  • TestNG's @Factory and JUnit Jupiter's @TestFactory are the same feature
  • TestNG's @BeforeTest is just JUnit Jupiter's @BeforeEach under another name
  • JUnit Jupiter's @TestMethodOrder skips later tests when an earlier one fails
  • TestNG's @Parameters maps onto JUnit Jupiter's @ParameterizedTest
  • JUnit Jupiter reads a testng.xml-style XML suite file