Maven Surefire plugin in JUnit - Build an Automation Script
package com.example; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class CalculatorTest { @Test void addition_shouldReturnCorrectSum() { Calculator calc = new Calculator(); int result = calc.add(2, 3); assertEquals(5, result, "2 + 3 should equal 5"); } } // Calculator.java package com.example; public class Calculator { public int add(int a, int b) { return a + b; } } // pom.xml snippet for Surefire plugin /* <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </build> */
This code defines a simple JUnit 5 test class CalculatorTest with one test method addition_shouldReturnCorrectSum. It tests the add method of the Calculator class.
The test uses assertEquals to check that adding 2 and 3 returns 5. This assertion ensures the test passes only if the method works correctly.
The pom.xml snippet shows how to configure the Maven Surefire plugin version 3.0.0, which runs the JUnit tests when you execute mvn test.
Running mvn test in the project directory will invoke Surefire, which finds and runs the test, then reports the result in the console.
This setup follows best practices: using JUnit 5 annotations, clear assertions, and proper Maven plugin configuration.
Now add data-driven testing with 3 different input pairs for the add method
Practice
Solution
Step 1: Understand the role of Maven Surefire plugin
The Maven Surefire plugin is designed to run tests automatically during the build process, ensuring code correctness early.Step 2: Differentiate from other Maven plugins
Other plugins like compiler or deploy handle compiling or deployment, not running tests.Final Answer:
To run tests automatically during the build process -> Option CQuick Check:
Maven Surefire runs tests during build [OK]
- Confusing Surefire with compiler plugin
- Thinking Surefire packages the app
- Assuming Surefire deploys the app
pom.xml file?Solution
Step 1: Identify the correct plugin artifactId for Surefire
The Maven Surefire plugin usesmaven-surefire-pluginas its artifactId.Step 2: Confirm groupId and artifactId match Surefire plugin
The groupId isorg.apache.maven.plugins, which is standard for Maven plugins.Final Answer:
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId></plugin> -> Option DQuick Check:
Surefire plugin artifactId = maven-surefire-plugin [OK]
- Using maven-compiler-plugin instead of surefire
- Confusing JUnit engine with Surefire plugin
- Using maven-jar-plugin for testing
pom.xml snippet, what will happen when you run mvn test?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>Solution
Step 1: Understand the includes configuration
The<includes>tag specifies which test files to run. Here, it includes files matching**/*Test.java.Step 2: Interpret the pattern meaning
The pattern means any Java file ending with 'Test.java' in any folder will be run as a test.Final Answer:
Only Java files ending with 'Test.java' will be run as tests -> Option AQuick Check:
Includes pattern '**/*Test.java' runs matching test files [OK]
- Assuming all Java files run as tests
- Thinking files starting with 'Test' run
- Believing version 3.0.0-M7 is invalid
pom.xml but tests are not running. Which of these is the most likely cause?Solution
Step 1: Check plugin artifactId spelling
If the artifactId is misspelled, Maven cannot find the plugin, so tests won't run.Step 2: Consider other causes
Missing version usually defaults to latest, unsaved pom.xml is unlikely, and test folder location matters but less common cause.Final Answer:
The artifactId is misspelled as maven-surefire-plugn -> Option BQuick Check:
Misspelled artifactId stops Surefire plugin loading [OK]
- Ignoring typo in artifactId
- Assuming missing version always breaks build
- Not verifying test source folder
@Fast annotation using Maven Surefire plugin. Which configuration snippet in pom.xml achieves this?Solution
Step 1: Understand Surefire's group filtering
Surefire uses<groups>to include tests with specific JUnit categories or tags.Step 2: Match the correct tag for including groups
Using<groups>Fast</groups>runs only tests tagged with @Fast.Final Answer:
<configuration><groups>Fast</groups></configuration> -> Option AQuick Check:
Use <groups> to run tagged tests [OK]
- Using excludedGroups instead of groups
- Filtering by filename instead of annotation
- Using <test> tag incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
