我无法获得Java11中使用Jacoco maven插件进行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>
这些是我的插件详细信息(请注意,这些设置在Java8中工作得非常好):
<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.
如您所见,在离线检测的情况下不会生成jaoco. exec文件。
请注意,当我不使用离线检测(即使用准备代理目标)时,我能够生成Jacoco. exec文件,但它会忽略PowerMock测试。
根据这个问题:https://github.com/jacoco/jacoco/issues/663
版本0.8.3提供了对Java11的支持,但是我不确定是否对导致任何问题的离线检测进行了任何更改。
如果还需要我做什么,请告知。
将Powermock版本更新到2.0.7修复了这个问题。我现在能够使用离线检测生成jaoco. exec文件。
除了离线仪器,还要确保正确的版本已设置为Java编译器的-source和-target。这也会影响覆盖率报告,因为有时只有离线仪器设置无助于获得电源模拟测试覆盖率。
<properties>
....
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
.....
</properties>
..
..
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</plugin>