我无法使用 Jacoco maven 插件获得 Java 11 中用于 Powermock 测试的代码覆盖率。我的版本详细信息如下:
Java version: jdk-11.0.2
Maven version: maven 3.5.2
Jacoco maven plugin version: 0.8.5
Powermock version: 2.0.0-beta.5
我还在所有子项目的类路径中添加了 jacoco 运行时代理:
<dependencies>
<!-- Added to provide jacocoagent in classpaths for offline instrumentation builds
Refer https://www.jacoco.org/jacoco/trunk/doc/offline.html -->
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<scope>test</scope>
<version>${jacoco.plugin.version}</version>
</dependency>
</dependencies>
这些是我的插件详细信息(请注意,这些设置在 Java 8 中工作得非常好):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Skips unit tests if the value of skipTests property is true -->
<skipTests>${skipTests}</skipTests>
<!-- Excludes integration tests when unit tests are run -->
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
<systemPropertyVariables>
<jacoco-agent.destfile>${project.build.directory}/target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<append>true</append>
<destFile>${user.dir}/target/jacoco.exec</destFile>
<excludes>
<exclude>*</exclude>
</excludes>
<dataFile>${user.dir}/target/jacoco.exec</dataFile>
<outputDirectory>${user.dir}/target/coverage-reports/jacoco</outputDirectory>
</configuration>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
结果:
[INFO] --- jacoco-maven-plugin:0.8.5:instrument (default-instrument) @ utils ---
[WARNING] The POM for org.codehaus.plexus:plexus-utils:jar:1.1 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.5:restore-instrumented-classes (default-restore-instrumented-classes) @ utils ---
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.5:report (jacoco-site) @ utils ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
如您所见,在离线检测的情况下不会生成 jacoco.exec 文件。
请注意,当我不使用离线检测(即使用 prepare-agent 目标)时,我能够生成 jacoco.exec 文件,但它忽略了 PowerMock 测试。
根据这个问题: https ://github.com/jacoco/jacoco/issues/663
版本 0.8.3 中提供了对 Java 11 的支持,但是我不确定是否在导致任何问题的脱机工具中进行了任何更改。
请告知我是否还需要其他任何东西。