首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java 11中不适用于PowerMock测试的Jacoco脱机工具

问在Java 11中不适用于PowerMock测试的Jacoco脱机工具
EN

Stack Overflow用户
提问于 2020-06-08 04:01:01
回答 2查看 3K关注 0票数 0

我无法使用Jacoco插件在Java 11中获得Powermock测试的代码覆盖率。我的版本详情如下:

代码语言:javascript
复制
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运行时代理,就像我们应该在所有子项目的类路径中一样:

代码语言:javascript
复制
<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中运行得非常好):

代码语言:javascript
复制
    <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>

结果:

代码语言:javascript
复制
[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文件。

请注意,当我不使用离线检测(即使用准备代理目标)时,我能够生成jacoco.exec文件,但是它忽略了PowerMock测试。

如本期所示:https://github.com/jacoco/jacoco/issues/663

在0.8.3版中提供了对Java 11的支持,但我不确定离线测试中是否有任何导致问题的更改。

如果我需要其他的话,请通知我。

EN

回答 2

Stack Overflow用户

发布于 2020-06-26 02:59:27

将Power模仿版本更新为2.0.7解决了这个问题。我现在能够使用离线检测工具生成jacoco.exec文件。

票数 0
EN

Stack Overflow用户

发布于 2021-12-29 19:25:00

除了脱机工具之外,还要确保将正确的版本设置为Java的-source和-target。这也会影响覆盖率报告,因为有时只有脱机设备设置才有助于获得电源模拟测试覆盖范围。

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62254805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档