有没有人能够从 Maven 构建中获得在 JaCoCo 中工作的 JMockit 和 Powermock 单元测试的单元测试覆盖率?

我有一个现有的 Powermock 单元测试测试集,我想逐渐迁移到 JMockit。但是我需要能够在一份报告中看到所有单元测试的测试覆盖率,最好是在声纳中。

通过将 JaCoCo 置于“离线”模式,我确实让 JMockit 和 Powermock 测试与 Surefire/JaCoCo 一起运行(否则我遇到了一个问题,其中一个代理在测试结束时没有被终止,然后 mvn clean 不能在下次运行时删除生成的 target\surefire\surefirebooter2967126910681005991.jar )。但是没有为 JMockit 测试生成覆盖率。

如果你有这个工作,请从你的 pom 中发布一些摘录。

这是我的 pom 的样子(注意 surefire 插件配置了 reuseForks=false 以解决 Powermock 中的 PermGen 内存泄漏,这是迁移到 JMockit 的主要原因之一)

        <profile> 
        <!-- use this profile to perform Sonar analysis --> 
        <id>sonar</id> 
        <properties> 
            <sonar.language>java</sonar.language> 
            <!-- Tells Sonar to use the generated test coverage report --> 
            <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis> 
            <!-- Tells Sonar to use JaCoCo as the code coverage tool --> 
            <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin> 
            <sonar.java.codeCoveragePlugin>jacoco</sonar.java.codeCoveragePlugin> 
        </properties> 
        <build> 
            <plugins> 
                <!-- surefire (junit) plugin config with JaCoCo listener --> 
                <plugin> 
                    <groupId>org.apache.maven.plugins</groupId> 
                    <artifactId>maven-surefire-plugin</artifactId> 
                    <version>2.16</version> 
                    <configuration> 
                        <!-- note: use single JVM to append to JaCoCo coverage file --> 
                        <forkCount>1</forkCount> 
                        <reuseForks>false</reuseForks> 
                        <argLine>-XX:MaxPermSize=256m </argLine> 
                        <systemPropertyVariables> 
                            <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile> 
                        </systemPropertyVariables> 
                    </configuration> 
                </plugin> 
 
                <!-- JaCoCo (Sonar) plugin config--> 
                <plugin> 
                    <groupId>org.jacoco</groupId> 
                    <artifactId>jacoco-maven-plugin</artifactId> 
                    <version>0.6.3.201306030806</version> 
                    <executions> 
                        <execution> 
                            <id>instrument</id> 
                            <phase>process-classes</phase> 
                            <goals> 
                                <goal>instrument</goal> 
                            </goals> 
                        </execution> 
                        <execution> 
                            <id>restore</id> 
                            <phase>site</phase> 
                            <goals> 
                                <goal>restore-instrumented-classes</goal> 
                                <goal>report</goal> 
                            </goals> 
                        </execution> 
                        <execution> 
                            <id>check</id> 
                            <goals> 
                                <goal>check</goal> 
                            </goals> 
                            <configuration> 
                                <rules> 
                                    <rule> 
                                        <element>BUNDLE</element> 
                                        <limits> 
                                            <limit> 
                                                <counter>COMPLEXITY</counter> 
                                                <value>COVEREDRATIO</value> 
                                                <minimum>0.0</minimum> 
                                            </limit> 
                                        </limits> 
                                    </rule> 
                                </rules> 
                            </configuration> 
                        </execution> 
                    </executions> 
                    <configuration> 
                        <append>true</append> 
                    </configuration> 
                </plugin> 
            </plugins> 
        </build> 
    </profile> 

请您参考如下方法:

在类路径上的未检测类或 jacoco agent.jar 未添加到测试类路径之后,已通过检测类。

要检查两种可能性,请检查已检测类的位置,运行带有 -X 标志的 mvn 并检查类路径以进行测试执行(以查看类路径元素的顺序以及 jacoco 代理是否在类路径上。)


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!