java - Jacoco Maven 离线检测 - Tomcat

标签 java tomcat integration-testing jacoco jacoco-maven-plugin

我正在尝试获取集成测试的代码覆盖率报告。 Jacoco maven 插件能够为单元测试提供代码覆盖率,但为集成测试提供 0% 的覆盖率。集成测试正在访问应用程序的其余 api 端点,该应用程序已部署在 tomcat 中。

我的 Maven jacoco 插件和 Surefire 插件看起来像这样。

<plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>prepare-agent-integration</id>
                    <goals>
                        <goal>prepare-agent-integration</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-unit-test</id>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${project.build.directory}/target/jacoco-it.exec</dataFile>
                        <outputDirectory>${project.build.directory}/target/jacoco-ut</outputDirectory>
                    </configuration>
                </execution>
            </executions>

            <configuration>
                <systemPropertyVariables>
                    <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
                </systemPropertyVariables>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20</version>
            <configuration>
                <!-- <skip>true</skip> -->
                <!-- <systemPropertyVariables> <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile> 
                    </systemPropertyVariables> -->
            </configuration>
            <!-- <configuration> <skip>true</skip> </configuration> -->
            <executions>
                <execution>
                    <id>unit-tests</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <!-- Never skip running the tests when the test phase is invoked -->
                        <!-- <skip>true</skip> -->
                        <argLine>@{argLine}
                            -javaagent:c:\\iat\\mavenrepository\\org\\jacoco\\org.jacoco.agent\\0.7.10-SNAPSHOT\\org.jacoco.agent-0.7.10-SNAPSHOT-runtime.jar=destfile=C:\\Users\\dmahapat\\Workspaces\\MyEclipse
                            2016 CI\\JaxRsApp\\target\\jacoco.exec</argLine>
                        <includes>
                            <include>**/*UnitTest.java</include>
                        </includes>
                        <excludes>
                            <exclude>**/*IntegrationTest.java</exclude>
                        </excludes>
                    </configuration>
                </execution>
                <execution>
                    <id>integration-tests</id>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <!-- Never skip running the tests when the integration-test phase 
                            is invoked -->
                        <!-- argLine>-javaagent:$WORKSPACE/target/lib/jacoco-agent-0.7.9.jar=includes=*,destfile=*/jacoco-coverage.exec,append=false</argLine -->
                        <skip>false</skip>
                        <argLine>@{argLine}
                            -javaagent:c:\\iat\\mavenrepository\\org\\jacoco\\org.jacoco.agent\\0.7.10-SNAPSHOT\\org.jacoco.agent-0.7.10-SNAPSHOT-runtime.jar=destfile=C:\\Users\\dmahapat\\Workspaces\\MyEclipse
                            2016 CI\\JaxRsApp\\target\\jacoco-it.exec
                        </argLine>

                        <includes>
                            <include>**/*IntegrationTest.java</include>
                        </includes>
                        <excludes>
                            <exclude>**/*UnitTest.java</exclude>
                        </excludes>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我正在测试阶段执行单元测试,并在集成测试阶段执行集成测试。 我收到的最新错误是“由于缺少执行数据文件而跳过 JaCoCo 执行。”

最佳答案

引用documentation of prepare-agent-integration :

Same as prepare-agent, but provides default values suitable for integration-tests:

  • bound to pre-integration-test phase
  • different destFile

引用documentation of prepare-agent :

Prepares a property pointing to the JaCoCo runtime agent that can be passed as a VM argument to the application under test. Depending on the project packaging type by default a property with the following name is set:

  • tycho.testArgLine for packaging type eclipse-test-plugin and
  • argLine otherwise.

在大多数情况下,argLine 由启动 JVM 执行单元测试的 maven-surefire-plugin 自动选取 - http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#argLine

argLine: Arbitrary JVM options to set on the command line.

这解释了为什么您会获得单元测试的覆盖。

要获得集成测试的覆盖率,您必须确保此属性传递给被测应用程序的 JVM,即执行 Tomcat 的 JVM,这完全取决于您启动它的方式。

关于java - Jacoco Maven 离线检测 - Tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46369308/

相关文章:

java - 错误 :Cause: failed to find target with hash string 'Google Inc.:Google APIs:23' in: E:\AndroidStudio\SDK

java - 简单字符串拆分

tomcat - 如何保护我的微服务,该服务只能从亚马逊的 api 网关或任何经过身份验证的应用程序访问

unit-testing - 单元测试逻辑、集成测试数据

python - Ruby 的 VCR 库是否有 python 替代品?

java - 缺少 "ice:column"的开始标记

java - 当存在 2 个版本的库时,按需在 tomcat 中加载类?

java - 在catalina.bat中适当的地方设置JAVA_OPTS

c# - 确保在 C# 集成测试后所有线程都已停止

java - 对文件系统上的 XML 文件进行快速全文搜索。选择什么策略?