集成 JaCoCo、Arquillian 和 REST Assured 使我的代码覆盖率为 0%

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

我正在使用 JaCoCo、Arquillian 放心地测试我的 RESTFul API。问题是我的 JaCoCo 报告中的代码覆盖率为 0%。我正在为测试用例使用 Maven 配置文件,获取一个 wildfly 实例,将其部署在 JVM 上,部署我的 API 然后 运行 测试。

我做了一些研究,我知道 JaCoCo 必须在同一个 JVM 上 运行,其中 .war 文件是 运行(我的情况是什么).

由于 JaCoCo 在同一个 JVM 上,我认为覆盖率不会发生任何变化,但它给我带来了 0%。

谁能帮我解决这个问题?

在 pom.xml 上关注我的 maven 配置文件:

<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>

我必须在 Arquillian 启动容器时配置 JaCoCo 代理。 实际上,我的 jacoco 插件变成了:

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>

在 arquillian.xml 参数中,在“javaVmArguments”属性 末尾添加 ${jacoco.agent} 变量。像这样:

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