1. Introduction

In this tutorial, we'll examine the Maven Surefire Plugin.

It is the plugin that runs the unit tests in a Maven project.

2. The Goals

Let's start with the plugin goals.

2.1. test

When we execute 'mvn test', Maven runs our unit tests. In other words, we tell Maven to execute the test phase of the default build lifecycle on our project. Maven does this by executing the plugin goals for each phase up to the test phase. We should note that each lifecycle phase is composed of multiple plugin goals. As we can guess, the Maven Surefire Plugin has the test goal which is bound to the test phase.

So we'll conclude that surefire:test is the plugin goal that runs our unit tests.

We can invoke this plugin by specifying a phase that is greater than or equal to test:

mvn clean test
mvn test
mvn verify

Alternatively, we can invoke the plugin goal directly:

mvn surefire:test

If the classes aren't compiled, we can compile them first and then call the plugin goal:

mvn test-compile surefire:test

2.2. help

Surefire also provides the help goal to display help information.

For example, the following command displays the argument descriptions for the test goal.

mvn surefire:help -Ddetail=true -Dgoal=test

3. Configuration

Now, we'll look at the basic configuration of the Surefire plugin:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <test>*Test,Test*,E2E*</test>
            </configuration>
        </plugin>
    </plugins>
</build>

Here, we're modifying the general configuration of the plugin by setting the test patterns.

Surefire provides many configuration options that enable us to fine-tune the test execution. For example, we can

4. Summary

In this quick tutorial, we've looked at what Maven Surefire Plugin is. We also briefly examined its goals and configuration options.

Finally, the source code for all examples is available on Github.