TExcelTAD
Home › Java Builds: Maven & Gradle › Testing with Surefire & JUnit 5

Testing with Surefire & JUnit 5

The test phase is bound to the Surefire plugin. It compiles nothing itself: test-compile has already built src/test/java into target/test-classes. Surefire finds the test classes, starts the JUnit Platform in a forked JVM with the test classpath, and fails the build if a test fails. Integration tests have a sibling plugin, Failsafe, bound to integration-test and verify.

JUnit 5 in one dependency

JUnit 5 is three things: the Platform (the launcher that tools such as Surefire, Gradle and IDEs talk to), Jupiter (the API you write tests with, plus its engine) and Vintage (runs old JUnit 4 tests). The aggregator junit-jupiter brings the API, parameterized tests and the engine. Surefire 3 detects the Platform on the test classpath by itself.

bash · try it yourself
mkdir -p calc/src/main/java/demo calc/src/test/java/demo && cd calc
cat > src/main/java/demo/Calc.java <<'SRC'
package demo;

public class Calc {
    public static int add(int a, int b) { return a + b; }
    public static int percent(int part, int whole) { return part * 100 / whole; }
}
SRC
cat > src/test/java/demo/CalcTest.java <<'SRC'
package demo;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class CalcTest {
    @Test
    void adds() {
        assertEquals(5, Calc.add(2, 3));
    }

    @ParameterizedTest
    @CsvSource({"1, 4, 25", "3, 4, 75", "4, 4, 100"})
    void percents(int part, int whole, int expected) {
        assertEquals(expected, Calc.percent(part, whole));
    }

    @Test
    void percentOfZeroFails() {
        assertThrows(ArithmeticException.class, () -> Calc.percent(1, 0));
    }
}
SRC
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>calc</artifactId>
  <version>1.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.14.4</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <pluginManagement>
      <plugins>
        <plugin><artifactId>maven-compiler-plugin</artifactId><version>3.16.0</version></plugin>
        <plugin><artifactId>maven-surefire-plugin</artifactId><version>3.6.0</version></plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
POM
mvn test | sed -n '/--- surefire/,/^\[INFO\] BUILD/p'
ls target/surefire-reports
[INFO] --- surefire:3.6.0:test (default-test) @ calc ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running demo.CalcTest
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.095 s -- in demo.CalcTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
TEST-demo.CalcTest.xml
demo.CalcTest.txt

Surefire counts each parameterized case as a test (5 = 1 + 3 + 1). The surefire-reports folder holds a plain-text summary and a JUnit XML file per class: the XML is what CI servers (Jenkins, GitLab, GitHub test reporters) parse to show test results.

When a test fails

Break percent (integer maths done in the wrong order) and look at what Maven prints:

bash · try it yourself
mkdir -p calc/src/main/java/demo calc/src/test/java/demo && cd calc
cat > src/main/java/demo/Calc.java <<'SRC'
package demo;

public class Calc {
    public static int percent(int part, int whole) { return part / whole * 100; }
}
SRC
cat > src/test/java/demo/CalcTest.java <<'SRC'
package demo;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

class CalcTest {
    @Test
    void quarter() { assertEquals(25, Calc.percent(1, 4)); }

    @Test
    void whole() { assertEquals(100, Calc.percent(4, 4)); }
}
SRC
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>calc</artifactId>
  <version>1.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.14.4</version><scope>test</scope></dependency>
  </dependencies>
  <build><pluginManagement><plugins>
    <plugin><artifactId>maven-surefire-plugin</artifactId><version>3.6.0</version></plugin>
  </plugins></pluginManagement></build>
</project>
POM
mvn test | grep -E 'Tests run|FAIL|ERROR\]   |expected'
echo "exit code: ${PIPESTATUS[0]}"
echo "--- run one test, then skip them"
mvn test -Dtest='CalcTest#whole' | grep -E 'Tests run:|BUILD'
mvn package -DskipTests | grep -E 'Tests are skipped|BUILD'
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.040 s <<< FAILURE! -- in demo.CalcTest
[ERROR] demo.CalcTest.quarter -- Time elapsed: 0.006 s <<< FAILURE!
org.opentest4j.AssertionFailedError: expected: <25> but was: <0>
[ERROR]   CalcTest.quarter:8 expected: <25> but was: <0>
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0
[INFO] BUILD FAILURE
exit code: 1
--- run one test, then skip them
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.033 s -- in demo.CalcTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
[INFO] Tests are skipped.
[INFO] BUILD SUCCESS
  • The failure names the class, the method and the assertion (expected: <25> but was: <0>), and Maven's exit code is 1, which is what fails a CI job.
  • -Dtest=Class#method runs a subset (patterns such as -Dtest='*IT' work too).
  • -DskipTests compiles the tests but doesn't run them; -Dmaven.test.skip=true doesn't even compile them. Both belong on a developer's command line, never in a release pipeline.

Useful Surefire settings

xml
<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.6.0</version>
  <configuration>
    <runOrder>alphabetical</runOrder>                <!-- stable class order, see Field Notes -->
    <failIfNoSpecifiedTests>false</failIfNoSpecifiedTests> <!-- -Dtest in a multi-module build -->
    <excludedGroups>slow</excludedGroups>            <!-- skip @Tag("slow") tests -->
    <argLine>-Duser.timezone=UTC</argLine>           <!-- JVM options of the forked test JVM -->
  </configuration>
</plugin>

Note: JUnit 6 (6.1.3 is in the local repository) keeps the Jupiter API: switching is a version bump to 6.1.3, with Java 17 as the minimum and a few long-deprecated APIs removed. The Platform and Jupiter versions are aligned in 6 (both 6.x), while JUnit 5 pairs Jupiter 5.14 with Platform 1.14, as the dependency tree showed.

Test yourself

Which plugin runs unit tests in Maven's test phase?

  1. maven-compiler-plugin
  2. maven-surefire-plugin
  3. maven-failsafe-plugin
  4. maven-jar-plugin

What is the difference between -DskipTests and -Dmaven.test.skip=true?

  1. None
  2. -DskipTests compiles tests but doesn't run them; maven.test.skip skips compiling them too
  3. -DskipTests also skips packaging
  4. maven.test.skip only skips failing tests

What do CI servers read to display Maven test results?

  1. The console log
  2. The JUnit XML files in target/surefire-reports
  3. target/classes
  4. The POM