How do I configure Maven Surefire for JUnit tests?

To configure Maven Surefire for running JUnit tests, add the Maven Surefire Plugin to the <build> section of your pom.xml.

For modern JUnit 5 tests, you typically need:

  1. A JUnit 5 dependency
  2. The Maven Surefire Plugin
  3. Test classes named using Maven’s default test naming patterns

1. Add JUnit 5 Dependency

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.13.4</version>
        <scope>test</scope>
    </dependency>
</dependencies>

2. Configure Maven Surefire Plugin

Add this inside your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.5.3</version>
        </plugin>
    </plugins>
</build>

A complete minimal Maven setup looks like this:

<project>
    <!-- project coordinates omitted -->

    <properties>
        <maven.compiler.release>25</maven.compiler.release>
        <junit.jupiter.version>5.13.4</junit.jupiter.version>
        <maven.surefire.version>3.5.3</maven.surefire.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.version}</version>
            </plugin>
        </plugins>
    </build>
</project>

3. Use Standard Test Class Names

By default, Surefire runs tests whose names match patterns like:

**/Test*.java
**/*Test.java
**/*Tests.java
**/*TestCase.java

For example:

import org.junit.jupiter.api.Test;

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

class CalculatorTest {

    @Test
    void shouldAddTwoNumbers() {
        int result = 2 + 3;

        assertEquals(5, result);
    }
}

4. Run the Tests

From the project directory, run:

mvn test

Maven will compile your test code and run tests using Surefire.

5. Optional: Include or Exclude Specific Tests

You can customize which tests Surefire runs:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.5.3</version>
    <configuration>
        <includes>
            <include>**/*Test.java</include>
            <include>**/*Tests.java</include>
        </includes>
        <excludes>
            <exclude>**/*IntegrationTest.java</exclude>
        </excludes>
    </configuration>
</plugin>

6. Optional: Run Tests by Tag

If you use JUnit 5 tags:

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

class PaymentServiceTest {

    @Test
    @Tag("fast")
    void shouldProcessPayment() {
        // test code
    }
}

Configure Surefire like this:

<configuration>
    <groups>fast</groups>
</configuration>

Or run from the command line:

mvn test -Dgroups=fast

Summary

For JUnit 5, the essential Maven Surefire configuration is:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.13.4</version>
    <scope>test</scope>
</dependency>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.5.3</version>
</plugin>

Then run:

mvn test