The Maven Surefire plugin runs your tests automatically during the build. It helps check if your code works as expected before you finish your project.
Maven Surefire plugin in JUnit
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<!-- optional settings here -->
</configuration>
</plugin>This XML goes inside the <plugins> section of your pom.xml file.
You can add options inside <configuration> to control which tests run and how.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin><plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0</version> <configuration> <includes> <include>**/*Test.java</include> </includes> </configuration> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>This example shows a simple Calculator class with a test. The Maven Surefire plugin runs the test when you build with mvn test. The test checks if adding 2 and 3 equals 5.
public class Calculator { public int add(int a, int b) { return a + b; } } import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test public void testAdd() { Calculator calc = new Calculator(); assertEquals(5, calc.add(2, 3)); } } // pom.xml snippet: // <plugin> // <groupId>org.apache.maven.plugins</groupId> // <artifactId>maven-surefire-plugin</artifactId> // <version>3.0.0</version> // </plugin>
Surefire automatically finds tests by default if they follow naming patterns like *Test.java or Test*.java.
You can customize which tests run by changing the <includes> and <excludes> in the configuration.
Surefire generates reports in the target/surefire-reports folder after tests run.
The Maven Surefire plugin runs your tests during the build process.
It helps catch problems early by checking your code automatically.
You configure it in your pom.xml file inside the <plugins> section.