0
0
JUnittesting~15 mins

Maven Surefire plugin in JUnit - Build an Automation Script

Choose your learning style9 modes available
Run JUnit tests using Maven Surefire plugin
Preconditions (4)
Step 1: Open terminal or command prompt
Step 2: Navigate to the project root directory containing pom.xml
Step 3: Run the command 'mvn test' to execute tests
Step 4: Observe the console output for test execution results
Step 5: Verify that all JUnit tests run and pass successfully
✅ Expected Result: Maven Surefire plugin runs all JUnit tests and reports test results with no failures.
Automation Requirements - JUnit 5 with Maven Surefire plugin
Assertions Needed:
Verify that the test method executes and passes
Verify that Maven Surefire plugin reports test success in console output
Best Practices:
Use @Test annotation for test methods
Use assertions like Assertions.assertEquals or Assertions.assertTrue
Configure Maven Surefire plugin in pom.xml with correct version
Run tests via 'mvn test' command
Keep test methods independent and repeatable
Automated Solution
JUnit
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.

Common Mistakes - 4 Pitfalls
Not configuring the Maven Surefire plugin in pom.xml
Using JUnit 4 annotations with JUnit 5 dependencies
Writing test methods without assertions
{'mistake': "Running tests with 'java' command instead of 'mvn test'", 'why_bad': "Tests won't run properly because Surefire handles test discovery and execution.", 'correct_approach': "Use 'mvn test' to run tests with Maven Surefire plugin."}
Bonus Challenge

Now add data-driven testing with 3 different input pairs for the add method

Show Hint