Quick revision
The short answer
The IDE runs whatever we click on, while Maven Surefire runs only what its configuration discovers, so the usual causes are configuration filters. Surefire includes classes by name pattern, Test*, *Test, *Tests and *TestCase by default, so OrderIT or OrderSpec are skipped; a suiteXmlFiles setting restricts the run to the classes listed there; groups in the pom exclude tests the IDE runs; and a mix of JUnit and TestNG providers can hide one framework. We confirm by comparing test counts and running the class with -Dtest=ClassName.
What to remember
In this answer, we saw that the IDE runs whatever we select, while Surefire runs only what passes its filters, and that a missing class almost always fell through one of them: the default name patterns that skip *IT and *Spec classes, a suiteXmlFiles setting that limits the run to the suite, pom groups, or a mix of JUnit and TestNG providers. Comparing test counts and running the class with -Dtest tells us which, and a CI check on test counts keeps it from happening again.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The IDE runs whatever we click on, while Maven Surefire runs only what its configuration discovers, so the usual causes are configuration filters. Surefire includes classes by name pattern, Test*, *Test, *Tests and *TestCase by default, so OrderIT or OrderSpec are skipped; a suiteXmlFiles setting restricts the run to the classes listed there; groups in the pom exclude tests the IDE runs; and a mix of JUnit and TestNG providers can hide one framework. We confirm by comparing test counts and running the class with -Dtest=ClassName.
Why this question is asked
Missing tests are worse than failing tests. A failing test at least tells us something is wrong; a test that never runs in the build gives a green result while checking nothing, and it can go unnoticed for months. The puzzle in this question, green in IntelliJ, absent in Maven, is common because the two tools discover tests in completely different ways. Interviewers ask it to see whether we debug systematically instead of guessing. In this answer, we will recap how each tool decides what to run, go through the causes in the order we would check them, and finish with a check that stops this from happening again.
1How the IDE and Maven Choose Tests
When we right-click a class in IntelliJ and choose Run, the IDE runs exactly that class with its own TestNG integration. It does not apply Surefire's name patterns, it ignores the pom's group filters unless we configure them in the run configuration, and it does not care whether the class is listed in a suite file. If the code compiles and has @Test methods, it runs.
Maven works the other way round. In the test phase, Surefire scans the compiled test classes, keeps those that match its include patterns, removes those that match its exclude patterns, applies any suite file and group filters, and hands the result to the provider for the framework it detected. A class that falls through any of those filters is silently skipped, with no warning in the log. So the question is always: which filter removed this class?
- Is suiteXmlFiles configured?YesOnly classes in the suite runNoCheck name patterns
- Does the class name match Surefire's includes?YesCheck groupsNoSkipped: rename or add an include
- Do pom groups or excludedGroups filter it?YesSkipped: check its groupsNoCheck providers
- Are JUnit and TestNG both on the classpath?YesOne framework may be hiddenNoRun with -Dtest and read the log
4 steps in order. Play them, or step through them one at a time.
2Check 1: Class Name Patterns
By default, Surefire includes classes whose names match **/Test*.java, **/*Test.java, **/*Tests.java and **/*TestCase.java. A class named OrderServiceTest runs; a class named OrderServiceIT, OrderSpec or OrderChecks does not. The IT suffix is deliberate: by convention, classes ending in IT are integration tests run by the Maven Failsafe plugin in the integration-test and verify phases, so mvn test never runs them and mvn verify only does if Failsafe is configured.
Notice the comment in the configuration: setting <includes> replaces the default list rather than adding to it, so a configuration that only lists *Spec.java would suddenly skip every *Test class. Renaming the class to follow the convention is often the simpler fix.
3Check 2: A Suite File in the pom
If the Surefire configuration has <suiteXmlFiles>, the name patterns no longer matter; only the classes, packages and groups the suite file lists are run. A new class added to the codebase but not to testng.xml runs perfectly from the IDE and never in the build. This is one of the most common causes in older TestNG projects, and the fix is either to add the class, to switch the suite to <packages> so new classes are picked up automatically, or to drop the suite file and rely on patterns and groups.
4Check 3: Groups Configured in the pom
Surefire's <groups> and <excludedGroups> settings filter TestNG groups. If the pom says <groups>unit</groups>, a test without the unit group does not run in Maven, although the IDE runs it happily. The same applies in reverse with <excludedGroups>slow</excludedGroups> and a class marked slow. A related trap is a configuration method without alwaysRun = true: when groups are filtered, it may be skipped, so the tests in the class are skipped too, and they show up as skips rather than as missing.
5Check 4: Mixed Providers and Engines
If the project has both JUnit and TestNG on the test classpath, Surefire chooses one provider, and the other framework's tests are not discovered. Typical signs are that all the TestNG classes are missing while the JUnit classes run, or the reverse. The same can happen with mismatched versions of JUnit Platform artifacts, where the launcher cannot load an engine. The fix is to run both frameworks on the JUnit Platform with the TestNG engine, or to configure providers explicitly.
6Confirming the Cause
Instead of reading configuration and guessing, we can make Maven tell us. First, compare counts. The IDE shows how many tests ran; the Surefire log prints a line per class and a summary at the end.
If the class runs when named with -Dtest, the framework and provider are fine, and the cause is a filter: name patterns, a suite file or groups. If it does not run even then, the provider or engine is the problem. The -X debug output shows the effective configuration and the provider, which settles it. The difference between 455 and 412 here is exactly the 43 tests in PricingSpec, which is a quick way to know we found the whole problem.
7Preventing It From Coming Back
Once fixed, we add a guard. The simplest is a CI step that compares the number of executed tests with the previous build on the main branch and fails if it drops by more than a few percent without an explanation in the change. Another is a naming rule enforced in review or by a small build check: every class with @Test methods under src/test must match the Surefire or Failsafe patterns. Both are cheap, and they turn silent disappearances into visible failures.
8Follow-up questions
Why do integration tests named *IT not run with mvn test?
Because Surefire's default patterns exclude them and Failsafe is responsible for them. Failsafe runs in the integration-test phase, after packaging, and checks results in the verify phase, so mvn verify runs them if the Failsafe plugin is declared in the pom. Without it, *IT classes are never run by Maven at all.
Can an abstract base class or a class with a constructor argument cause this?
Yes. Surefire and TestNG skip abstract classes, and TestNG cannot create a test class without a usable constructor unless a @Factory provides instances. The IDE might run a concrete subclass we clicked on while the build skips the base, so it is worth checking that each test class can actually be instantiated.
Tests run in Maven but show as skipped. Is that the same problem?
No, skipped means they were discovered but not executed, usually because a configuration method failed, a dependency failed, or a test was disabled. The log or the TestNG report shows the reason for each skip. Missing tests do not appear in the report at all.
9Conclusion
In this answer, we saw that the IDE runs whatever we select, while Surefire runs only what passes its filters, and that a missing class almost always fell through one of them: the default name patterns that skip *IT and *Spec classes, a suiteXmlFiles setting that limits the run to the suite, pom groups, or a mix of JUnit and TestNG providers. Comparing test counts and running the class with -Dtest tells us which, and a CI check on test counts keeps it from happening again.
Open with
The key difference is that IntelliJ runs whatever I click on, while Surefire only runs what its configuration discovers, and anything filtered out is skipped silently. So I go through the filters.
Then, if they want more
First, class names: by default Surefire includes Test star, star Test, star Tests and star TestCase, so a class called OrderIT or PricingSpec never runs with mvn test. IT classes are meant for Failsafe in the verify phase.
Second, if the pom sets suiteXmlFiles, only what that testng.xml lists runs, so a new class that nobody added to the suite is missing. Third, groups and excludedGroups in the Surefire configuration filter TestNG groups, and a setup method without alwaysRun can be filtered along with them.
Fourth, if both JUnit and TestNG are on the classpath, Surefire picks one provider and the other framework's tests disappear. To confirm, I compare the counts, the IDE's number against the Tests run line in the Maven log, and then run mvn test with -Dtest equals the class name.
If it runs when named explicitly, the cause is a filter; if not, it is the provider or engine, and the -X output shows which provider was chosen. After fixing it, I would add a CI check that fails when the test count drops unexpectedly, so tests cannot vanish quietly again.
About this answer
- Last updated
- 12 November 2026
- 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.
- Official documentation
- JUnit user guide, Mockito documentation
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 "Tests pass in IntelliJ but in the Maven build some TestNG classes are not run at all....": https://scrivio-interview-library.netlify.app/java/testng/addb5013c3/