问题复现
在我司对核心业务基于Spock落地单元测试过程中, 使用jacoco插件生成单元测试报告,遇到了覆盖率异常的问题.
问题原因
经过排查, 最终发现在使用powermock的
@PrepareForTest([HttpUtils.class, XXXHandler.class]) 注解时, 数组里的类是会被Jacoco忽略的.
powermock和jacoco存在冲突,以下是抄来的解释:
JaCoCo和PowerMock都是通过在加载类的时候修改字节码文件来实现统计覆盖率和mock静态类的功能。JaCoCo在加载class的时候会把统计代码插入到class中,而PowerMock当使用了@PrepareForTest注解,在加载相关类的时候会从class文件重新读取字节码信息,导致JaCoCo的修改都没有了,所以就没办法统计到了
———————————————— 版权声明:本文为CSDN博主「dearfulan」的原创文章,遵循CC 4.0
BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35448165/article/details/108200453
一开始我根据该文章内容对代码进行修改, 由于这篇文章描述的并不完全,导致测试报告没有生成,因此我来发表一下我的解决办法.
解决办法
官方解决方案
基于官方解决方案,我对其进行了修改:
<dependencies>
<!--离线实际上使用了代理,java agent。-->
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>0.8.3</version>
<classifier>runtime</classifier>
</dependency>
</dependencies>
<build>
<!-- jacoco -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<configuration>
<fileSets>
<fileSet>
<directory>..</directory>
<include>**/*.exec</include>
</fileSet>
</fileSets>
<!--包含-->
<includes>
<include>com/**/*</include>
</includes>
<!--排除包-->
<!-- <excludes>-->
<!-- <exclude>com/**/*</exclude>-->
<!-- </excludes>-->
<!-- rules裏面指定覆蓋規則 -->
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<!--指定指令覆盖率达到90%-->
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.90</minimum>
</limit>
<!--指定行覆盖率至少为90%-->
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>90%</minimum>
</limit>
<!-- 指定方法覆盖最低90% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>METHOD</counter>
<value>COVEREDRATIO</value>
<minimum>0.90</minimum>
</limit>
<!-- 指定分支覆盖最低90% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.90</minimum>
</limit>
<!-- 指定類覆蓋到100%,不能遺失任何類 -->
<!-- 指定类覆盖到,最多Missed 0 -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
</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>
<!--clean+verify 生成报告-->
<execution>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<!--检查代码覆盖率-->
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<!--设置执行器-->
<systemPropertyVariables>
<jacoco-agent.destfile>${project.build.directory}/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
参考资料
https://www.jianshu.com/p/99ee50a9fe47
https://blog.csdn.net/qq_35448165/article/details/108200453
https://blog.csdn.net/lxlmycsdnfree/article/details/78425944
https://blog.csdn.net/yangbo10086/article/details/89198626?utm_medium
觉得有用不要忘了留下个👍