javamavenjboss-arquillianjacocorest-assured

Integrating JaCoCo, Arquillian and REST Assured brings me a code coverage of 0%


I'm using JaCoCo, Arquillian and rest-assured to test my RESTFul API. The problem is that I'm getting a 0% code coverage within my JaCoCo report. I'm using a maven profile for test cases, getting a wildfly instance, deploying it on JVM, deploying my API and then running the tests.

I've made some researches and reach the point where I know that JaCoCo must be running on same JVM where the .war file is running (What is my case).

As JaCoCo is on the same JVM, I thought the coverage wouldn't suffer any changes, but it brings me 0%.

Can anyone help me with this?

Follows my maven profile on pom.xml:

<profile>
        <id>test-postgresql</id>

        <properties>
            <datasource.driver>postgresql.jar</datasource.driver>
            <datasource.driverClass>org.postgresql.Driver</datasource.driverClass>
            <datasource.url>jdbc:postgresql://localhost:5432/security-test</datasource.url>
            <datasource.user>postgres</datasource.user>
            <datasource.password>a</datasource.password>
        </properties>

        <dependencies>
            <!-- WildFly Container Managed -->
            <dependency>
                <groupId>org.wildfly</groupId>
                <artifactId>wildfly-arquillian-container-managed</artifactId>
                <version>${wildfly.version}</version>
                <scope>test</scope>
            </dependency>

            <!-- PostgreSQL JDBC Driver -->
            <dependency>
                <groupId>postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>${jdbc.postgresql.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <!-- Dependency Plugin -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>${plugin.dependency.version}</version>
                    <executions>
                        <!-- Unpack WildFly -->
                        <execution>
                            <id>unpack-wildfly</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>unpack</goal>
                            </goals>
                            <configuration>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>org.wildfly</groupId>
                                        <artifactId>wildfly-dist</artifactId>
                                        <version>${wildfly.version}</version>
                                        <type>zip</type>
                                        <overWrite>false</overWrite>
                                        <outputDirectory>target</outputDirectory>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>

                        <!-- Deploy JDBC Driver -->
                        <execution>
                            <id>copy-driver</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>postgresql</groupId>
                                        <artifactId>postgresql</artifactId>
                                        <version>${jdbc.postgresql.version}</version>
                                        <type>jar</type>
                                        <overWrite>false</overWrite>
                                        <outputDirectory>target/wildfly-${wildfly.version}/standalone/deployments</outputDirectory>
                                        <destFileName>${datasource.driver}</destFileName>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <!-- Skip Unit Tests -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${plugin.surefire.version}</version>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>

                <!-- Integration Tests -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>${plugin.failsafe.version}</version>
                    <configuration>
                        <argLine>${jacoco.agent.it.arg}</argLine>
                        <redirectTestOutputToFile>false</redirectTestOutputToFile>
                        <systemPropertyVariables>
                            <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
                            <jboss.home>${project.basedir}/target/wildfly-${wildfly.version}</jboss.home>
                            <module.path>${project.basedir}/target/wildfly-${wildfly.version}/modules</module.path>
                        </systemPropertyVariables>
                        <skipTests>false</skipTests>
                        <includes>
                            <include>**/test/**/*.java</include>
                        </includes>
                    </configuration>
                    <executions>
                        <execution>
                            <id>integration-test</id>
                            <goals>
                                <goal>integration-test</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>verify integration-test</id>
                            <goals>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <!-- Liquibase Plugin (Drop Database) -->
                <plugin>
                    <groupId>org.liquibase</groupId>
                    <artifactId>liquibase-maven-plugin</artifactId>
                    <version>${liquibase.version}</version>
                    <configuration>
                        <driver>${datasource.driverClass}</driver>
                        <url>${datasource.url}</url>
                        <username>${datasource.user}</username>
                        <password>${datasource.password}</password>
                        <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                    </configuration>
                    <executions>
                        <execution>
                            <id>drop-database</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>dropAll</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <!-- JaCoCo Plugin -->
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>${plugin.jacoco.version}</version>
                    <dependencies>
                        <!-- JaCoCo -->
                        <dependency>
                            <groupId>org.jacoco</groupId>
                            <artifactId>org.jacoco.core</artifactId>
                            <version>${plugin.jacoco.version}</version>
                        </dependency>

                        <!-- Arquillian JaCoCo -->
                        <dependency>
                            <groupId>org.jboss.arquillian.extension</groupId>
                            <artifactId>arquillian-jacoco</artifactId>
                            <version>1.0.0.Alpha8</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <append>true</append>
                    </configuration>
                    <executions>
                        <execution>
                            <id>jacoco-agent</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>prepare-agent-integration</goal>
                            </goals>
                            <configuration>
                                <destFile>${sonar.jacoco.itReportPath}</destFile>
                                <propertyName>jacoco.agent.it.arg</propertyName>
                                <append>true</append>
                            </configuration>
                        </execution>

                        <execution>
                            <id>jacoco-report</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>report-integration</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

Solution

  • I've had to configure the JaCoCo agent when Arquillian started the container. Actually, my jacoco plugin becomes:

    pom.xml

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${plugin.jacoco.version}</version>
        <executions>
            <execution>
                <id>jacoco-prepare</id>
                <phase>validate</phase>
                <goals>
                    <goal>prepare-agent-integration</goal>
                </goals>
                <configuration>
                    <propertyName>jacoco.agent</propertyName>
                </configuration>
            </execution>
            <execution>
                <id>jacoco-report</id>
                <phase>post-integration-test</phase>
                <goals>
                    <goal>report-integration</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    In the arquillian.xml arguments, add the ${jacoco.agent} variable at the end of "javaVmArguments" property. Like this:

    <property name="javaVmArguments">-Djboss.socket.binding.port-offset=10000 -Xms512m -Xmx1024m -XX:MaxPermSize=512m ${arquillian.debug} ${jacoco.agent}</property>