简体   繁体   English

PowerMockito 的声纳覆盖报告

[英]Sonar coverage report for PowerMockito

I'm using PowerMockito and @PrepareForTest annotation for my test class.我正在为我的测试类使用 PowerMockito 和 @PrepareForTest 注释。 When I do this, Sonar says none of the branches have been covered.当我这样做时,声纳说没有一个分支被覆盖。 However, my other test classes that don't use PowerMockito works well.但是,我的其他不使用 PowerMockito 的测试类运行良好。 here is my pom looks like这是我的 pom 看起来像

<dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>1.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.6.2 </version>
            <scope>test</scope><plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>3.2</version>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.6.201602180812</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <!-- <phase>prepare-package</phase> -->
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
        </dependency>

Do I need to add anything in this pom ?我需要在这个 pom 中添加任何东西吗? Any help is Appreciated, Thanks in Advance任何帮助表示赞赏,提前致谢

yes right powermock doesn't work with sonar in the first step, the cause of the problem is the combination of powermock and jacoco, there is already an issue related to this problem ( https://github.com/powermock/powermock/issues/727 ), see here for further informations:是的,正确的powermock在第一步中不能与声纳一起使用,问题的原因是powermock和jacoco的组合,已经有一个与此问题相关的问题( https://github.com/powermock/powermock/issues /727 ),请参阅此处了解更多信息:

https://github.com/powermock/powermock/wiki/Code-coverage-with-JaCoCo https://github.com/powermock/powermock/wiki/Code-coverage-with-JaCoCo

in the meantime you can try a workaround, as described here:同时,您可以尝试一种解决方法,如下所述:

https://www.geekality.net/2013/09/12/maven-package-runtime-dependencies-in-lib-folder-inside-packaged-jar/ https://www.geekality.net/2013/09/12/maven-package-runtime-dependencies-in-lib-folder-inside-packaged-jar/

  • PowerMock doesn't work with sonar in the first step, the cause of the problem is the combination of powermock and jacoco第一步PowerMock不配合声纳,问题原因是powermock和jacoco的结合
  • We will not face this issue locally, but if we use sonar for test coverage calculations, the error is as follows我们不会在本地面对这个问题,但是如果我们使用声纳进行测试覆盖率计算,则错误如下
Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null) 
  • In PowerMock 2.0.9 version, a workaround for this problem is provided they have added an annotation @PrepareOnlyThisForTest , this provides the ability for the isolation of a particular Test class.在 PowerMock 2.0.9 版本中,如果他们添加了注释@PrepareOnlyThisForTest ,则可以解决此问题,这提供了隔离特定测试类的能力。
  • Apart from that, we need to add delegate the Runner to SpringRunner using @PowerMockRunnerDelegate(SpringRunner.class)除此之外,我们需要使用@PowerMockRunnerDelegate(SpringRunner.class)将 Runner 委托添加到 SpringRunner

Add Power Mock Dependencies in pom.xml在 pom.xml 中添加 Power Mock 依赖项

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.9</version>
            <scope>test</scope>
        </dependency>

Use the PowerMock Junit Runner for test Class and declare the test class that we're mocking and delegate SpringRunner将 PowerMock Junit Runner 用于测试类并声明我们正在模拟的测试类并委托 SpringRunner

    @RunWith(PowerMockRunner.class)
    @PrepareOnlyThisForTest(ServiceApplication.class)
    @PowerMockRunnerDelegate(SpringRunner.class)
    public class ExampleControllerTest {
        PowerMockito.mockStatic(ServiceApplication.class);
        Mockito.when(ServiceApplication.getStatic()).thenReturn("");

I've found that if you have only one class in the @PrepareForTest annotation eg @PrepareForTest(ClassA.class) then jacoco coverage works.我发现如果@PrepareForTest 注释中只有一个类,例如@PrepareForTest(ClassA.class)那么jacoco 覆盖就可以工作。 It's only when there is more than one class eg @PrepareForTest({ClassA.class, ClassB.class}) that jacoco coverage is reported as 0%只有当有多个类时,例如@PrepareForTest({ClassA.class, ClassB.class}) ,jacoco 覆盖率才会报告为 0%

You just need to add the delegate the Runner to SpringRunner;您只需要将 Runner 委托添加到 SpringRunner;

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM