使用Jacoco对使用Powermock编写的测试类进行单元测试范围 [英] Unit tests coverage using Jacoco for test classes written using Powermock

查看:1170
本文介绍了使用Jacoco对使用Powermock编写的测试类进行单元测试范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在jenkins的sonarqube仪表板上获取代码覆盖率报告.即将发布代码覆盖率报告,但仅显示4.6%的覆盖率.经过调查,我发现使用PowerMocks编写的测试类已被跳过.

I am trying to get the code coverage report on the sonarqube dashboard on jenkins. The code coverage report is coming up but showing only 4.6% coverage. On investigating I found out that the test classes written using PowerMocks are getting skipped.

在进一步的调查中,我发现"JaCoCo在动态修改/创建的类上不能很好地发挥作用(这是powermock的工作方式.这是已知的限制,我们目前无法对此进行任何处理."

On further investigation I found that "JaCoCo doesn't play well with dynamically modified/created classes (this is the way how powermock works). This is a known limitation we can't currently do anything about".

是否有任何解决方法,以便我也可以获得使用PowerMocks编写的测试类的适当代码覆盖范围.

Is there any work around for this so that I can get proper code coverage for test classes written using PowerMocks too.

推荐答案

我已成功使用powermock-module-javaagent使用Jacoco生成了PowerMock覆盖范围.

I have managed to generate PowerMock coverage with Jacoco, using powermock-module-javaagent.

只需确保将Powermock代理放在 jacoco代理之后:

Just make sure you put powermock agent after jacoco agent:

<artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <useSystemClassLoader>true</useSystemClassLoader>
                <argLine>${jacocoArgLine} -javaagent:${settings.localRepository}/org/powermock/powermock-module-javaagent/${powermock.version}/powermock-module-javaagent-${powermock.version}.jar -noverify</argLine>
...

如果要查看示例,请看以下项目: https://github. com/jfcorugedo/sonar-scanner

If you want to see an example, take a look at this project: https://github.com/jfcorugedo/sonar-scanner

在这里您可以看到声纳将静态方法和PowerMock模拟的new语句考虑在内:

Here you can see that sonar takes into account static methods and new statements mocked by PowerMock:

如果要模拟new语句,请确保使用PowerMockRule而不是PowerMockRunner.

If you want to mock newstatements make sure you use PowerMockRule instead of PowerMockRunner.

看看此测试

这篇关于使用Jacoco对使用Powermock编写的测试类进行单元测试范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆