java jacoco powerMock 单元测试覆盖率为0怎么解决
我们项目中使用powerMock作为单元测试的mock工具,统计项目测试覆盖率使用jacoco编译的结果上传到sonar,但是jacoco 和 powerMock在运行时runtime加载代码的时候自定义了类加载器,所以就会有冲突,导致测试覆盖率为0。
使用命令 mvn clean verify sonar:sonar上传jacoco编译结果(这里sonar命令会自动调用jacoco-maven-plugin插件编译),出现错误:
[WARNING] Classes in bundle 'MyDemoProject' do not match with execution data. For report generation the same class files must be used as at runtime.
[WARNING] Execution data for class com/demo/controller/DemoController does not match.
问题定位:运行时加载类出现问题
解决问题:
找到jacoco 官方文档描述:https://www.eclemma.org/jacoco/trunk/doc/classids.html
其中:
What happens if different classes are used at runtime and at analysis time?
In this case execution data cannot be related to the analyzed classes. As a consequence such classes are reported with 0% coverage.
What can cause different class ids?
Even if the class files on the file system are the same there is possible that classes seen by the JaCoCo runtime agent are different anyways. This typically happens when another Java agent is configured before the JaCoCo agent or special class loaders pre-process the class files. Typical candidates are:
- Mocking frameworks
- Application servers
- Persistence frameworks
上面清晰的描述了测试覆盖率为0的原因,和使用mock框架的影响。
解决方式参考:https://www.eclemma.org/jacoco/trunk/doc/examples/build/pom-offline.xml
最终pom 正确配置如下:
<properties><jacoco.version>0.8.8</jacoco.version></properties><dependencies><dependency><!-- must be on the classpath --><groupId>org.jacoco</groupId><artifactId>org.jacoco.agent</artifactId><classifier>runtime</classifier><version>${jacoco.version}</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><systemPropertyVariables><jacoco-agent.destfile>${basedir}/../target/jacoco.exec</jacoco-agent.destfile></systemPropertyVariables></configuration></plugin><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>${jacoco.version}</version><configuration><destFile>${basedir}/../target/jacoco.exec</destFile><dataFile>${basedir}/../target/jacoco.exec</dataFile><output>file</output><append>true</append></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>default-report</id><goals><goal>report</goal></goals></execution></executions></plugin><build><plugins>
说明:
上面的pom.xml是多模块项目中的父pom,其中jacoco.exec需要包含所有子模块的扫描结果,所以配置的路径是:${basedir}/../target/jacoco.exec(所有子模块的结果都是存放在父(根)目录下的target目录中),并且设置了 <append>true</append> 所有子模块生成一个jacoco.exec文件中。
java jacoco powerMock 单元测试覆盖率为0怎么解决相关推荐
- Springboot 单元测试结合Jacoco收集单元测试覆盖率
目录 单元测试 Junit 简单例子 mockito Mock 的限制 Powermock maven 引入 注解使用 处理私有方法 mock构造函数 结合Springboot一起使用 各种runne ...
- Java变更行单元测试覆盖率
概述 Java应用代码的单元测试覆盖率工具Jacoco已经非常成熟及完善了.对于历史的Java项目很多都是没有单元测试的,往往在新的在迭代的过程中都希望能有变更代码行的单元测试覆盖率,从而提高迭代质量 ...
- sonar集成jacoco展示单元测试覆盖率
(一)sonar-scanner结合jacoco 这里还是要用到maven生成jacoco的统计结果,所以推荐直接使用maven的方式检查项目 1.在要检测的项目的pom文件中加入如下配置: < ...
- java 代码覆盖率_java单元测试覆盖率统计
很多时候,开发写了单元测试,但是我们并不知道当前的单元测试覆盖率是怎么样的,所以我们需要有技术手段来分析我们的单元测试覆盖率情况. 那么,jacoco来了... jacoco介绍: jacoco 是一 ...
- java代码实现单元测试jacoco覆盖率收集生成多模块聚合报告
文章目录 背景 一.准备工作 - 生成exec文件 二.准备工作 - 引入依赖 三.利用jenkins-jacoco插件源码收集覆盖率结果 四.生成jacoco报告文件,聚合多模块 附赠相关知识点 背 ...
- 基于Jacoco+Maven+Jenkins配置的单元测试覆盖率报告实践
最近,系统重构,要求对底层基础服务加入单元测试用例,而我被安排产出单元测试覆盖率报告. 第一版比较简单,只是一个简单的测试报告,并无具体覆盖率 1. 针对maven项目,pom配置 <repor ...
- SpringBoot-Jacoco单元测试覆盖率报告
SpringBoot-Jacoco单元测试覆盖率报告 添加Maven 添加测试依赖 <dependencies> <!--spring-boot测试--><depende ...
- java 中的单元测试_浅谈Java 中的单元测试
单元测试编写 Junit 单元测试框架 对于Java语言而言,其单元测试框架,有Junit和TestNG这两种, 下面是一个典型的JUnit测试类的结构 package com.example.dem ...
- testng查看覆盖率_使用Cobertura统计单元测试覆盖率
学习Cobertura的起因是前天晚上媳妇儿(终于有出场机会了)突然问我有没有用过一些可以统计单元测试覆盖率的工具,答曰:木有...感觉比较丢人,于是昨天晚上研究了一把,今天顺便share一下. 因为 ...
最新文章
- 了解过去与理解现在的一把钥匙
- Mysql函数:Last_insert_id()语法讲解
- Linux 命令查找指定文件夹下符合查询条件的文件和文件夹实例演示
- ElementUI 中日期选择器总结
- lenze变频器怎么更改地址_英威腾变频器GD300维修
- 使Safari在Windows Vista上每20秒停止崩溃
- JimuReport积木报表,一个好用的开源免费的报表平台
- 什么是提花?大提花面料与小提花面料如何区别?
- php结合nginx,mysql
- MATLAB曲线拟合函数
- 使用SmartUpload上传文件报错
- YDUI的移动端页面rem适配方案使用方法记录
- Dev-C++ 一直提示源文件未编译,原因及解决办法
- 美丽的夜,一个程序员的思考
- { 周赛 }又被虐爆了
- Zip4j 压缩包加密压缩与解压
- 操作系统(一):什么是操作系统
- PS2019污点修复画笔工具、修复画笔工具、修补工具、内容感知移动工具
- EXSI-VM安装和配置
- qimage 像素 替换颜色_米8精简版拥有24MP前置摄像头与使用超级像素技术索尼IMX576传感器...