[英]Unit tests coverage using Jacoco for test classes written using Powermock
I am trying to get the code coverage report on the sonarqube dashboard on jenkins.我正在尝试在 jenkins 的 sonarqube 仪表板上获取代码覆盖率报告。 The code coverage report is coming up but showing only 4.6% coverage.
代码覆盖率报告即将发布,但仅显示 4.6% 的覆盖率。 On investigating I found out that the test classes written using PowerMocks are getting skipped.
在调查时,我发现使用 PowerMocks 编写的测试类被跳过了。
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".在进一步调查中,我发现“JaCoCo 不能很好地与动态修改/创建的类一起使用(这就是 powermock 的工作方式)。这是我们目前无法做任何事情的已知限制”。
Is there any work around for this so that I can get proper code coverage for test classes written using PowerMocks too.是否有任何解决方法,以便我也可以为使用 PowerMocks 编写的测试类获得适当的代码覆盖率。
Simple answer: no, there isn't.简单的回答:不,没有。
Long answer - boils down to these options:长答案 - 归结为以下选项:
I have managed to generate PowerMock coverage with Jacoco, using powermock-module-javaagent
.我已经设法用 Jacoco 生成 PowerMock 覆盖率,使用
powermock-module-javaagent
。
Just make sure you put powermock agent after jacoco agent:只要确保在 jacoco 代理之后放置 powermock 代理:
<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>
...
If you want to see an example, take a look at this project: https://github.com/jfcorugedo/sonar-scanner如果你想看一个例子,看看这个项目: https : //github.com/jfcorugedo/sonar-scanner
Here you can see that sonar takes into account static methods and new
statements mocked by PowerMock:在这里你可以看到声纳考虑了静态方法和 PowerMock 模拟的
new
语句:
If you want to mock new
statements make sure you use PowerMockRule
instead of PowerMockRunner
.如果要模拟
new
语句,请确保使用PowerMockRule
而不是PowerMockRunner
。
What works for me is to remove this对我有用的是删除它
@RunWith(PowerMockRunner.class)
And add this in the class并将其添加到类中
@Rule
public PowerMockRule rule = new PowerMockRule();
Also need to add the dependency for powermockito junit4 rule.还需要为 powermockito junit4 规则添加依赖项。
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
This official page will help more to understand it.这个官方页面将有助于更多地理解它。 PowerMock
电源模拟
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.