SAGARFIVE

Tutorials

5.3 Maven Integration testing using Failsafe Plugin:

Maven Failsafe Plugin:

  • The Failsafe Plugin is designed to run integration tests and as the name implies, when it fails, it does so in a safe way.
  • The Failsafe Plugin has only two goals:
  • failsafe:integration-test runs the integration tests of an application.
  • failsafe:verify verifies that the integration tests of an application passed.
  • The Maven lifecycle has four phases for running integration tests:
  • pre-integration-test for setting up the integration test environment.
  • integration-test for running the integration tests.
  • post-integration-test for tearing down the integration test environment.
  • verify for checking the results of the integration tests.
  • Command : mvn test or mvn clean test
  • The Failsafe Plugin generates reports in two different file formats:
  • Plain text files (*.txt)
  • XML files (*.xml)
  • By default, these files are generated in ${basedir}/target/failsafe-reports/TEST-*.xml.

Pom.xml

<dependencies>
    <!– Test framework which will be used by Failsafe plugin. Version number is mandatory –>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <!– Disable unit tests –>
                <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

Commands:

CommandInformation
1mvn testrun tests(unit and integration tests) using a suitable unit testing framework
These tests should not require the code be packaged or deployed
2mvn -Dtest=TestCircle testRunning a Single a class
3mvn -Dtest=TestCircle#mytest testRunning a specific method in a class
4mvn install -DskipTests or
mvn install -Dmaven.test.skip=true
Directly running install command(install the package in local repository)

Comparison Maven surefire and failsafe plugins :

maven-surefire-pluginmaven-failsafe-plugin
Main purposeunit testsintegration tests
Bound to build phasetestpre-integration-testintegration-testpost-integration-testverify
Build fails in phasetestverify
Default wildcard pattern**/Test*.java**/*Test.java**/*TestCase.java**/IT*.java**/*IT.java**/*ITCase.java
Default output directory${basedir}/target/surefire-reports${basedir}/target/failsafe-reports