QA Interview Hub · Knowledge base · Quizzes · Українська

Java Core › The test project

Maven: pom.xml, scope, Surefire and Failsafe, running tests

Maven builds a project from the description in pom.xml: coordinates, dependencies, plugins and properties, and two plugins run the tests. Surefire runs unit tests in the test phase for classes named Test*, *Test, *Tests or *TestCase, while Failsafe runs integration tests (IT*, *IT) in integration-test; the verify phase checks the result, so run mvn verify. Test libraries get scope test.

Java · Project and build

Tests are green locally, but on CI Maven "ran nothing". Why was the class LoginSpec skipped?

Maven looks like "just a runner" until tests vanish from the build or CI fails for no visible reason. The cause is almost always not knowing how Maven decides which classes are tests and in which phase they run. This lesson assembles that picture.

How it works and why. Maven builds a project from the description in pom.xml: coordinates (groupId, artifactId, version), dependencies, plugins, properties. The standard directory layout is src/main/java, src/test/java, src/test/resources. In a tests-only project the code lives in src/test. The build is driven by a lifecycle: a sequence of phases, and a command runs all phases up to the one named.

Phases and who works in them

compile → test-compilebuilds code and teststest: Surefireunit tests: *Test, Test*packagebuilds the artifactintegration-test: Failsafeintegration: *IT, IT*verifyFailsafe checks the result, the build fails here
Figure 1. The command mvn verify goes through all phases up to verify, so it runs both Surefire and Failsafe.

Two plugins run tests, with different rules:

SurefireFailsafe
PurposeUnit testsIntegration tests
Default namesTest*, *Test, *Tests, *TestCaseIT*, *IT, *ITCase
Phasetestintegration-test, verify
Test failureThe build fails at onceThe build fails in verify, after post-integration-test
Commandmvn testmvn verify

The special role of Failsafe is that a failing integration test does not stop the build at once: the post-integration-test phase (stopping the test environment, containers) still runs, and the verify goal checks the result. That is why you should run mvn verify, not mvn integration-test: in the second case a failure does not stop the build and CI stays green. In UI or API automation projects Surefire alone is often enough, and Failsafe is needed when tests run after services are started.

Example: the pom and the runs. Test libraries (JUnit, AssertJ, REST Assured) have scope test: they are needed to compile and run tests and do not reach the artifact. Versions go into <properties> or a BOM (dependencyManagement) so they do not drift apart. Failsafe is attached explicitly:

<properties>
  <maven.compiler.release>21</maven.compiler.release>
  <assertj.version>3.27.3</assertj.version>   <!-- versions in one place -->
</properties>

<dependencies>
  <dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>${assertj.version}</version>
    <scope>test</scope>   <!-- does not reach the artifact -->
  </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
      <artifactId>maven-failsafe-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>integration-test</goal>
            <goal>verify</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Everyday commands worth knowing:

mvn test -Dtest=LoginTest                 # one class
mvn test -Dtest=LoginTest#validLogin      # one method
mvn test -Dgroups=api                     # JUnit 5/6 tags, TestNG groups
mvn test -DexcludedGroups=slow
mvn verify -Pstaging -Dbrowser=firefox    # a profile and a property
mvn -DskipTests package                   # compiles tests, does not run them
mvn -Dmaven.test.skip=true package        # does not even compile tests
mvn dependency:tree                       # where a conflicting version comes from

A profile (<profile>) changes dependencies, properties or plugin configuration for an environment. Properties from -D are passed by Surefire into the test process, so the code reads them directly:

String browser = System.getProperty("browser", "chrome");
String baseUrl = System.getProperty("base.url", "https://stage.example.test");

Limits and alternatives.

  • Class names decide everything: LoginTests matches *Tests, while LoginSpec or CheckoutCheck is invisible to Surefire. You can add your own pattern through <includes>, but sticking to the suffix is simpler.
  • For JUnit 5/6 Surefire works through the JUnit Platform: tags are enabled with the groups parameter, which is not the same as TestNG groups though the property is the same.
  • -DskipTests compiles tests and skips running; -Dmaven.test.skip=true does not even compile them. On CI the second hides test compilation errors.
  • -Dtest=… in a multi-module project may fail if one module has no such test. Then add -Dsurefire.failIfNoSpecifiedTests=false.
  • Rerunning failed tests (surefire.rerunFailingTestsCount) exists, but it only masks instability: use it as a temporary measure with a visible "flaky" mark in the report.
  • Use the wrapper (mvnw) so the Maven version is the same for everyone instead of a globally installed one.

Typical mistakes.

  • Running mvn integration-test instead of mvn verify and not noticing failures.
  • A test dependency without scope test: JUnit ends up in the artifact.
  • A test class named outside the patterns: the build says "0 tests run".
  • Library versions scattered around the pom: updating one breaks another.
  • Fixing a dependency conflict by guessing instead of mvn dependency:tree.

Check yourself

Why run mvn verify and not mvn integration-test?

Answer

Failsafe only runs the tests in the integration-test phase and checks the result in verify, after post-integration-test (environment cleanup). Without verify a failure of integration tests will not stop the build.

How do -DskipTests and -Dmaven.test.skip=true differ?

Answer

The first compiles tests but does not run them. The second neither compiles nor runs them, so errors in test code go unnoticed.

The class CheckoutSpec sits in src/test/java, yet Maven says "No tests to run". Why?

Answer

By default Surefire takes only classes matching Test*, *Test, *Tests, *TestCase. The name CheckoutSpec matches none of them: rename the class or add your own pattern in includes.

In the interview. Junior: Maven builds a project from pom.xml: dependencies, plugins, properties. Test libraries have scope test. Surefire runs unit tests in the test phase, Failsafe runs integration tests (*IT) in integration-test, and verify checks the result, so I run mvn verify. Middle/Senior: I keep versions in properties or a BOM and look at the dependency tree with dependency:tree. I separate runs with profiles and tags (-Dgroups) and pass environment parameters through -D. I use the wrapper so the Maven version matches on CI and locally, and I avoid skip flags on CI so as not to hide compilation errors.

More articles in this chapter