简体   繁体   English

使用 Jacoco 对使用 Powermock 编写的测试类进行单元测试

[英]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:长答案 - 归结为以下选项:

  • have look into this Wiki page by the PowerMock team - maybe maybe "offline instrumentation" works out for you.已经查看了 PowerMock 团队的这个 Wiki 页面——也许“离线检测”可能适合你。
  • hope that the corresponding bug gets fixed at some point (I wouldn't hold my breath on that)希望相应的错误在某个时候得到修复(我不会屏住呼吸)
  • get rid of your dependency to PowerMock(ito) - by refactoring and improving your production code摆脱对 PowerMock(ito) 的依赖——通过重构和改进你的生产代码
  • [ I think I evaluated various coverage tools long time ago; [我我很久以前就评估过各种覆盖工具; and there was one commercial one that claims to work even with PowerMock.并且有一个商业广告声称即使使用 PowerMock 也能正常工作。 But I don't recall any specifics.但我不记得任何细节。 So I am basically saying: there might be a minuscule chance that another, proprietary coverage tool works with PowerMock ]所以我基本上是说:有可能是一个微不足道的机会,另外,独有覆盖工具的工作原理与PowerMock]

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

Take a look at this test看看这个测试

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.

相关问题 使用Powermock时,Gradle Jacoco不会跟踪Spock测试的覆盖范围 - Gradle Jacoco does not track the coverage of Spock test when using Powermock 使用gradle生成单个单元测试的Jacoco代码覆盖率报告 - Generate Jacoco code coverage report for individual unit tests using gradle 如何使用jacoco获得不同回购中测试的测试覆盖率? - How to get test coverage of tests in different repo using jacoco? 如何使用 Jacoco 从覆盖分析中排除测试类 - How to exclude test classes form Coverage Analysis using Jacoco 使用 JaCoCo 对 java.lang 类进行单元测试覆盖 - Unit test coverage on java.lang classes with JaCoCo 使用 Jacoco 进行 Cucumber 测试的代码覆盖率 - Code Coverage for Cucumber Tests using Jacoco 单元测试和集成测试的 Jacoco 覆盖率与覆盖率相结合 - Jacoco coverage of unit and integration tests combined with coverageratio 如何使用PowerMock和Jacoco Offline Instrumentation从Maven Multiproject获得SonarQube的覆盖范围 - How to get the coverage in SonarQube from Maven Multiproject using PowerMock and Jacoco Offline Instrumentation 使用Jacoco或IntelliJ IDEA生成单独的覆盖率报告以进行测试 - Generate separate coverage reports for tests using Jacoco or IntelliJ IDEA 将 JaCoCo 与 SONAR 集成以实现单元和集成测试覆盖率 - Integrating JaCoCo with SONAR for unit and integration test coverage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM