提问者:小点点

maven中具有Jacoco代码覆盖率的Powermock


遵循https://github.com/jfcorugedo/sonar-scanner/blob/master/pom.xml中的说明,也使用了Powermock留档中提到的Jacoco离线检测,但是当我使用以下配置时,代码覆盖率仅显示Power mock代码覆盖率,不显示其他非powermock(mockito和Junit)覆盖率。

有没有一种方法可以同时实现powermock、mockito和Junit代码覆盖率

下面是我的pom文件,它不会显示powermock覆盖范围。

<properties>
       <powermock.version>2.0.9</powermock.version>
       <jacoco.version>0.8.5</jacoco.version>
    </properties>
  <dependencies>
    <dependencies>
            
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.4</version>
            <scope>test</scope>
        </dependency>
        <!-- Power Mock -->
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-javaagent</artifactId>
            <version>${powermock.version}</version>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4-rule-agent</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build> 
     <plugins>
        <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>PACKAGE</element>
                                    <limits>
                                        <limit>
                                            <counter>LINE</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>0.0</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    </plugins>
    </build>

下面是适用于Powermock但忽略所有其他测试用例的POM文件(Jacoco脱机检测)。argline命令适用于surefire 2.19.1,但似乎不适用于更高版本。

    <properties>
       <powermock.version>2.0.9</powermock.version>
       <jacoco.version>0.8.5</jacoco.version>
    </properties>
  <dependencies>
            
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.4</version>
            <scope>test</scope>
        </dependency>
        <!-- Power Mock -->
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-javaagent</artifactId>
            <version>${powermock.version}</version>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4-rule-agent</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <plugins>
      <plugin>
       <groupId>org.jacoco</groupId>
       <artifactId>jacoco-maven-plugin</artifactId>
       <version>${jacoco.version}</version>
       <executions>
          <!--Jacoco offline instrumentation-->
          <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>report</id>
             <phase>prepare-package</phase>
             <goals>
                <goal>report</goal>
             </goals>
             <configuration>
                <dataFile>${project.build.directory}/coverage.exec</dataFile>
             </configuration>
          </execution>
          <execution>
             <id>jacoco-check</id>
             <goals>
                <goal>check</goal>
             </goals>
             <configuration>
                <rules>
                   <rule>
                      <element>PACKAGE</element>
                      <limits>
                         <limit>
                            <counter>LINE</counter>
                            <value>COVEREDRATIO</value>
                            <minimum>0.0</minimum>
                         </limit>
                      </limits>
                   </rule>
                </rules>
             </configuration>
          </execution>
       </executions>
    </plugin>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.19.1</version>
     <configuration>
        <argLine>
        -javaagent:${settings.localRepository}/org/powermock/powermock-module-javaagent/${powermock.version}/powermock-module-javaagent-${powermock.version}.jar
         </argLine>
     <systemPropertyVariables>
     <jacoco-agent.destfile>${project.build.directory}/coverage.exec</jacoco-agent.destfile>
     </systemPropertyVariables>
     </configuration>
      </plugin>
    </plugins>

已经被这个问题困住了很长一段时间,我尝试在mavensu火插件配置中添加@{argline}注释以及javaagent命令,但似乎也不起作用。我正在处理的代码需要powermock,因为它们有多个异步调用,需要使用with News进行模拟。使用cobretura也不是一个选项。


共1个答案

匿名用户

除了离线仪器,我们还需要确保正确的版本已设置为Java编译器的-source和-target。这似乎也会影响覆盖率报告,因为有时只有离线仪器设置无助于获得电源模拟测试覆盖率。

<properties>
        ....
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        .....
</properties>   
..
..

<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                    <configuration>
                  <source>${maven.compiler.source}</source>
                  <target>${maven.compiler.target}</target>
                </configuration>
</plugin>