déc 13

In this article, I will explain you how to add to your Maven build some Integration Tests executed by SoapUI.

As a bonus, Sonar Code Coverage will be enhanced with these tests.


Testing

Pre-requisites

logo-maven

Maven 3 is required because some of the below plugins have this requirement.
If you need to keep a Maven 2 build for your dev team, put all this configuration in a Maven profile dedicated to your Sonar build (ex: Profile integration).

Explanations

Here are the different plugins and configurations needed to succeed.

Cobertura dependency

<dependencies>
    <!-- needed to execute Integration tests instrumented with Cobertura -->
    <dependency>
        <groupId>net.sourceforge.cobertura</groupId>
        <artifactId>cobertura</artifactId>
        <version>1.9.4</version>
        <scope>test</scope>
    </dependency>
</dependencies>

This dependency is needed in order to instrument the classes with Cobertura.

Maven SoapUI plugin

Soapui-logo

The complete documentation for this plugin may be found here : Maven SoapUI plugin.

<plugin>
    <groupId>eviware</groupId>
    <artifactId>maven-soapui-plugin</artifactId>
    <version>4.5.1</version>
    <configuration>
        <junitReport>true</junitReport>
        <exportAll>true</exportAll>
        <projectFile>${project.build.directory}/test-classes/my-soapui-project.xml</projectFile>
        <outputFolder>${project.build.directory}/surefire-reports</outputFolder>
        <testSuite>myIntegrationTests</testSuite>
        <testFailIgnore>true</testFailIgnore>
    </configuration>
    <executions>
        <execution>
            <id>wsn-server-test</id>
            <phase>integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
</plugin>

This plugin will execute a SoapUI test suite contained in a SoapUI project XML file.
If you want to filter (using Maven filtering) this file, use the projectFile property to load the project’s file from the target directory :

<projectFile>${project.build.directory}/test-classes/my-soapui-project.xml</projectFile>

Property testFailIgnore must be set to true in order to prevent a failing test to break the build.

In this example, the plugin will execute the myIntegrationTests test suite from the project file my-soapui-project.xml.

The plugin test execution must be bound to the integration-test phase.

Cobertura IT Maven plugin

The cobertura-it-maven-plugin is a fork of codehaus cobertura-maven-plugin which enable the so long waited integration test coverage feature MCOBERTURA-86.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-it-maven-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <formats>
            <format>xml</format>
            <format>html</format>
        </formats>
        <check>
            <haltOnFailure>false</haltOnFailure>
        </check>
    </configuration>
    <executions>
        <execution>
            <id>cobertura-clean</id>
            <phase>clean</phase>
            <goals>
                <goal>clean</goal>
            </goals>
        </execution>
        <execution>
            <id>cobertura-instrument</id>
            <phase>process-classes</phase>
            <goals>
                <goal>instrument</goal>
            </goals>
        </execution>
        <execution>
            <id>cobertura-report</id>
            <phase>pre-site</phase>
            <goals>
                <goal>report-only</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The XML output report is mandatory for the Sonar analysis. The HTML one may be used by the development team but it’s optional.

Do not forget to set haltOnFailure in order to prevent a test failure to stop the build.

3 executions must be defined :

  • Goal clean must be called to clear instrumenting in the clean Maven phase.
  • Goal instrument must be executed in the process-classes in order to instrument the classes.
  • Goal report-only must be executed in the pre-site phase.

Now, let’s configure a Jetty server to start just before integration test execution and stop just after them.

Jetty Maven plugin

jetty-logo-80x22

Beware : this plugin must not be mixed with the maven-jetty-plugin.
This one is the new one, respecting the Maven convetion naming.

The complete documentation may be found here : jetty-maven-plugin.

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.1.2.v20120308</version>
    <configuration>
        <stopPort>18043</stopPort>
        <stopKey>STOP</stopKey>
        <connectors>
            <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <port>18042</port>
            </connector>
        </connectors>
    </configuration>
    <executions>
        <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
            <configuration>
                <!-- Needed in order to to code coverage on SoapUI tests -->
                <classesDirectory>${project.build.directory}/generated-classes/cobertura</classesDirectory>
                <useTestScope>true</useTestScope>
                <scanIntervalSeconds>0</scanIntervalSeconds>
                <daemon>true</daemon>
                <contextPath>/myIntegrationTestsContext</contextPath>
            </configuration>
        </execution>
        <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Attribute classesDirectory must be specified to use the instrumented classes.

The goal start is a new one introduced by the new version of the Jetty plugin. It starts a Jetty server without first executing the build up to “test-compile” phase.
Do not forget to set the attribute true in order to force Jetty to execute only while Maven is running, instead of running indefinitely.

Maven phases pre-integration-test and post-integration-test are used to start and stop the Jetty server.

Reporting

The reporting phase is mandatory to generate the Cobertura report. A bug prevents the Cobertura report to be generated in the integration-test phase.

<reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-it-maven-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <formats>
                    <format>html</format>
                    <format>xml</format>
                </formats>
            </configuration>
            <reportSets>
                <reportSet>
                    <reports>
                        <report>report-only</report>
                    </reports>
                </reportSet>
            </reportSets>
        </plugin>
    </plugins>
</reporting>

Sonar configuration

In order to make Sonar reuse reports, configure these properties in your Maven build :


<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.surefire.reportsPath>${project.build.directory}/surefire-reports</sonar.surefire.reportsPath>
<sonar.cobertura.reportPath>${project.build.directory}/site/cobertura/coverage.xml</sonar.cobertura.reportPath>

Jenkins configuration

A quite special configuration must be done in your Jenkins build : a Post Step must be added.
This step must launch mvn site command in order to produce the Cobertura report.

maven-IT

maven-IT-2

Complete code

<dependencies>
    <!-- needed to execute Integration tests instrumented with Cobertura -->
    <dependency>
        <groupId>net.sourceforge.cobertura</groupId>
        <artifactId>cobertura</artifactId>
        <version>1.9.4</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>eviware</groupId>
            <artifactId>maven-soapui-plugin</artifactId>
            <version>4.5.1</version>
            <configuration>
                <junitReport>true</junitReport>
                <exportAll>true</exportAll>
                <projectFile>${project.build.directory}/test-classes/my-soapui-project.xml</projectFile>
                <outputFolder>${project.build.directory}/surefire-reports</outputFolder>
                <testSuite>myIntegrationTests</testSuite>
                <testFailIgnore>true</testFailIgnore>
            </configuration>
            <executions>
                <execution>
                    <id>wsn-server-test</id>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-it-maven-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <formats>
                    <format>xml</format>
                    <format>html</format>
                </formats>
                <check>
                    <haltOnFailure>false</haltOnFailure>
                </check>
            </configuration>
            <executions>
                <execution>
                    <id>cobertura-clean</id>
                    <phase>clean</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
                <execution>
                    <id>cobertura-instrument</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                </execution>
                <execution>
                    <id>cobertura-report</id>
                    <phase>pre-site</phase>
                    <goals>
                        <goal>report-only</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>8.1.2.v20120308</version>
            <configuration>
                <stopPort>18043</stopPort>
                <stopKey>STOP</stopKey>
                <connectors>
                    <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                        <port>18042</port>
                    </connector>
                </connectors>
            </configuration>
            <executions>
                <execution>
                    <id>start-jetty</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
                        <!-- Needed in order to to code coverage on SoapUI tests -->
                        <classesDirectory>${project.build.directory}/generated-classes/cobertura</classesDirectory>
                        <useTestScope>true</useTestScope>
                        <scanIntervalSeconds>0</scanIntervalSeconds>
                        <daemon>true</daemon>
                        <contextPath>/myIntegrationTestsContext</contextPath>
                    </configuration>
                </execution>
                <execution>
                    <id>stop-jetty</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-it-maven-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <formats>
                    <format>html</format>
                    <format>xml</format>
                </formats>
            </configuration>
            <reportSets>
                <reportSet>
                    <reports>
                        <report>report-only</report>
                    </reports>
                </reportSet>
            </reportSets>
        </plugin>
    </plugins>
</reporting>

Complement

Here is another article dealing with Integration Tests in Maven builds : Let’s Turn Integration Tests with Maven to a First-Class Citizen.

Leave a Reply


Creative Commons License
Blog Infin-It par Infin-It est mis à disposition selon les termes de la licence Creative Commons Paternité-Pas d'Utilisation Commerciale 2.0 France.