Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

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?

Maven Surefire runs TestNG tests through its TestNG provider, which it picks automatically when org.testng:testng is on the test classpath, and suiteXmlFiles can point it at a testng.xml; Gradle runs them with test { useTestNG() }, one framework per Test task by default. Adding JUnit Jupiter can make one framework silently run nothing, because Surefire chooses one provider and Gradle uses one framework per task. The fix is to run both on the JUnit Platform with the TestNG engine from the JUnit team, or to configure the providers explicitly.

Why this question is asked

Running tests is supposed to be the boring part of a build. It stops being boring the day someone adds a JUnit 5 dependency to a TestNG project, usually through a Spring Boot starter, and the build output says that zero tests ran, or that only the new JUnit tests ran while the 800 TestNG tests quietly disappeared. Interviewers ask this because it tests whether we understand how build tools discover and launch tests rather than just which command to type. In this answer, we will recap how Maven and Gradle run TestNG, see why mixing in JUnit Jupiter breaks it, fix it with the JUnit Platform and the TestNG engine, and finish with IDE support and the reports CI reads.

1Running TestNG With Maven Surefire

Maven runs unit tests in the test phase with the Surefire plugin. Surefire does not run tests itself; it delegates to a provider, a small adapter for a specific framework. It chooses the provider by looking at the test classpath: if it finds TestNG, it uses the TestNG provider.

XML
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.11.0</version> <!-- check for the current version -->
<scope>test</scope>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> <!-- optional -->
</suiteXmlFiles>
</configuration>
</plugin>
mvn test # runs all classes matching Surefire's name patterns

Without suiteXmlFiles, Surefire finds test classes by name pattern, such as *Test and *Tests. With suiteXmlFiles, only what the suite lists runs, which is a common reason for tests that seem to vanish.

2Running TestNG With Gradle

In Gradle, the test task needs to be told which framework to use. useTestNG() selects TestNG, and its options block accepts group filters and suite files.

Java
dependencies {
testImplementation 'org.testng:testng:7.11.0'
}
test {
useTestNG() {
includeGroups 'smoke'
// suites 'src/test/resources/testng.xml'
}
}
./gradlew test # runs TestNG tests, only the smoke group here

A Gradle Test task runs one framework: useTestNG(), useJUnit() for JUnit 4, or useJUnitPlatform() for JUnit Jupiter and other Platform engines. Calling a second one replaces the first, which matters in the next step.

3Why Adding JUnit Jupiter Can Hide a Framework

Now someone adds spring-boot-starter-test, which brings JUnit Jupiter, and writes a new test with org.junit.jupiter.api.Test. With Maven, Surefire now sees both TestNG and the JUnit Platform on the classpath. It picks one provider, and the other framework's tests are simply not discovered; which one wins depends on the Surefire version and configuration, so check your version's provider-selection rules. The build reports success with fewer tests, or with Tests run: 0.

With Gradle, if the build still says useTestNG(), the new Jupiter tests never run; if someone changes it to useJUnitPlatform(), the TestNG tests stop running. Neither case produces an error, which is why it is so easy to miss.

What runs with both frameworks on the classpath
SetupTestNG testsJUnit Jupiter tests
Maven, default provider selectionMaybe (one provider is chosen)Maybe (one provider is chosen)
Gradle with useTestNG()RunNot run
Gradle with useJUnitPlatform() onlyNot runRun
JUnit Platform with testng-engineRunRun

4Fix: Run Both on the JUnit Platform

The JUnit Platform is the launcher layer of JUnit 5 and later. It does not care which framework a test uses, as long as there is an engine for it: the Jupiter engine runs JUnit Jupiter tests, and the TestNG engine, published by the JUnit team as org.junit.support:testng-engine, runs TestNG tests. With both engines on the classpath, one launcher discovers and runs everything.

XML
<!-- Maven: Surefire uses its JUnit Platform provider and runs both engines -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.support</groupId>
<artifactId>testng-engine</artifactId>
<version>1.0.5</version> <!-- check for the current version -->
<scope>test</scope>
</dependency>
// Gradle
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly 'org.junit.support:testng-engine:1.0.5'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
test { useJUnitPlatform() } // Jupiter and TestNG tests both run

Notice that TestNG itself stays as a dependency for compiling the old tests; the engine only lets the Platform run them. Group filtering then uses the Platform's tag filters, which the TestNG engine maps from TestNG groups. Some TestNG features that depend on testng.xml, such as suite-level parameters, may need adapting, so check the engine's documentation for what it supports. The alternative, configuring two Surefire executions with explicit providers or two Gradle Test tasks, also works but is more build code to maintain.

5IDEs and Reports

IntelliJ IDEA supports TestNG out of the box, and Eclipse through the TestNG plugin. Both can run a single method, a class, a package or a testng.xml file, and IntelliJ creates run configurations for suite files directly. This is one reason a test can pass in the IDE and not run in the build: the IDE runs what we click on, while the build runs what its configuration discovers.

For CI, TestNG writes its own testng-results.xml and HTML reports, and Surefire also writes JUnit-style XML files, one per class, in target/surefire-reports. CI servers such as Jenkins, GitHub Actions or GitLab read the JUnit XML format to show test counts, failures and durations. Comparing the test count in those reports before and after a dependency change is the quickest way to catch tests that silently stopped running.

6Follow-up questions

Why does the build pass when tests stop running?

Because zero failures is a success to Maven and Gradle. Surefire has a failIfNoTests option, and recent Gradle versions can fail a test task that discovers no tests at all (check your version), but neither notices when 800 out of 900 tests disappear. A CI check that compares the test count with the previous build catches that.

Is the TestNG engine a full replacement for running TestNG directly?

For most suites it is enough: tests, groups, data providers and dependencies work. Features tied to suite XML files and some listeners may behave differently, so we run both ways once and compare counts and results before switching the whole build.

How do you run one TestNG class from Maven?

With mvn test -Dtest=OrderServiceTest, or -Dtest=OrderServiceTest#placesOrder for one method. This overrides the include patterns, which also makes it a good check for whether a class is discovered at all.

7Conclusion

In this answer, we saw that Maven Surefire runs TestNG through a provider it picks from the classpath, optionally driven by suiteXmlFiles, and that Gradle runs it with useTestNG(), one framework per Test task. Adding JUnit Jupiter can make one framework vanish silently, because only one provider or framework is used. Running both on the JUnit Platform with the TestNG engine fixes it, IDEs run whatever we select, and the JUnit XML reports let CI compare test counts.

What to say in the interviewAbout 90 seconds aloud

Open with

With Maven, Surefire runs the tests in the test phase and delegates to a provider for the framework. If TestNG is on the test classpath, it picks the TestNG provider, and by default it finds classes by name patterns like Test suffix, or I can point suiteXmlFiles at a testng.xml, in which case only what the suite lists runs.

Then, if they want more

With Gradle, I configure the test task with useTestNG, and inside it I can set includeGroups or suites. A Gradle test task runs one framework at a time. The No tests executed problem comes from mixing frameworks.

When someone adds JUnit Jupiter, often through spring-boot-starter-test, Surefire sees both and picks one provider, so one framework's tests are silently not discovered, and in Gradle, useTestNG only runs TestNG and useJUnitPlatform only runs Jupiter. The build still passes because zero failures is success.

The clean fix is to run everything on the JUnit Platform: add the TestNG engine, org.junit.support testng-engine, from the JUnit team, next to the Jupiter engine, and use the Platform provider in Surefire or useJUnitPlatform in Gradle. Then one launcher runs both. In the IDE, IntelliJ or the Eclipse plugin can run single methods, classes or a testng.xml.

For CI, Surefire writes JUnit-style XML, plus TestNG's testng-results.xml, and I would compare test counts between builds to catch tests that quietly disappear.

About this answer

Last updated
12 November 2026
Versions it names
JUnit 5, JUnit 4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do you run TestNG tests with Maven and with Gradle, and why do some teams see 'No...": https://scrivio-interview-library.netlify.app/java/testng/99a5b98bab/