Maven Surefire Plugin

Last Updated : 18 Mar, 2026

Maven Surefire Plugin is used to run unit tests in a Maven project during the testing phase of the build lifecycle. It automatically executes test cases and generates test reports after execution.

  • Runs unit tests during the test phase
  • Generates reports in XML (default) and plain text (.txt) formats
  • Helps in analyzing test results after execution

Implementation of the Maven Surefire Plugin

Step 1: Add Surefire Plugin to pom.xml

Maven proposes defining the version of the plugin we wish to use in the pom.xml file.

Java
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version> 3.1.0- M7</version>
</plugin>

Step 2: Configure Surefire Plugin

The Surefire plugin supports JUnit and TestNG frameworks. Its behavior is consistent across both. You can control which tests to include or exclude using <includes> and <excludes> parameters.

Java
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.2.5</version> 
    <configuration>
        <!-- List of test classes to exclude from the test run -->
        <excludes>
            <exclude>DataTest.java</exclude> <!-- This file will be excluded from the test execution -->
        </excludes>
        <!-- List of test classes to include in the test run -->
        <includes>
            <include>DataCheck.java</include> <!-- Only this file will be included in the test execution -->
        </includes>
    </configuration>
</plugin>

Explanation:

  • It is simple to handle such circumstances, use the <includes> and <excludes> arguments to define the plugin.
  • The <include> parameter includes all the files named within it for unit testing.
  • Similarly, the <exclude> argument excludes files from the testing procedure.

Step 3: Run a Single Test Case

We can run a specific test class using the command:

mvn surefire:test -Dtest=TestCircle

Step 4: Skipping Tests on the Surefire Plugin

We can set the skipTests property to true in a project's POM file. This attribute will keep that project out of unit testing.

XML
<project>
    <build>
        <plugins>
            <!-- Surefire Plugin Configuration -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.1.0- M7</version>
                <configuration>
                    <skipTests>true</skipTests> <!-- Skip tests during the build -->
                </configuration>
            </plugin>

            <!-- Checkstyle Plugin Configuration -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>3.2.5</version>
                <configuration>
                    <configLocation>checkstyle.xml</configLocation> <!-- Path to your Checkstyle configuration -->
                    <failOnViolation>true</failOnViolation> <!-- Fail the build if violations are found -->
                </configuration>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- PMD Plugin Configuration -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.2.5</version>
                <configuration>
                    <rulesets>
                        <ruleset>/rulesets/java/quickstart.xml</ruleset>
                    </rulesets>
                </configuration>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

The implementation provided set up the Maven Surefire plugin for running and managing tests.

Comment
Article Tags:

Explore