首先是设置:我们在tomcat上将一个应用程序部署为带有几个Servlet的war文件,然后在jacoco代理处于活动状态时启动。我们运行的测试是集成测试,jacoco-it.exec文件是在tomcat shutdown上创建的,甚至包含数据。但是,即使我们的测试触及所有Servlet,只有一个类显示为“覆盖”,这是枚举的私有构造函数(由枚举以静态方式调用)。
更新inbetween:我现在注意到,“sessions”部分显示了所涵盖的所有类,它包含了所提到的枚举以及测试类,这是奇怪的,因为它们不应该被包含在内(它们没有戴上tomcat)。枚举也用于测试,这将解释它的涵盖
我在之前的项目中使用jacoco-integration-test-coverage完成了相同的程序,这一切都很好,所以我不知道是什么问题。
我尝试添加includeBootstrapClasses属性和其他一些调整(将dumpOnExit设置为true / false等),但没有一个工作。我甚至尝试过离线仪器,这只会产生相同的结果。将static {}
块添加到servlet的代码中,只是将“foo”打印到控制台也不会被标记为已覆盖,即使tomcat-log包含消息。
Jacoco config in pom:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<!-- Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Surefire plugin is executed. -->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${project.build.directory}/jacoco.exec</destFile>
<!-- Sets the name of the property containing the settings for JaCoCo
runtime agent. -->
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!-- Ensures that the code coverage report for unit tests is created
after unit tests have been run. -->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
</configuration>
</execution>
<!-- The Executions required by unit tests are omitted. -->
<!-- Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Failsafe plugin is executed. -->
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${project.build.directory}/jacoco-it.exec</destFile>
<!-- Sets the name of the property containing the settings for JaCoCo
runtime agent. -->
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<!-- Ensures that the code coverage report for integration tests after
integration tests have been run. -->
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
failsafeArgLine(在config中使用)如下:
-javaagent:/home/jenkins/.m2/repository/org/jacoco/org.jacoco.agent/0.7.2.201409121644/org.jacoco.agent-0.7.2.201409121644-runtime.jar=destfile=/home/jenkins/jenkins/jobs/dms-coverage/workspace/com.example.dms/target/jacoco-it.exec
surefireArgLine并不重要,因为现在没有单元测试,只有集成测试。
请注意,jacoco可以检测东西,但不是tomcat上的类,而是测试类。
如果您有任何想法,这里有什么问题,请给我一个提示。
谢谢和问候, 凯