Test coverage for both Powermock and JMockit unit tests
Asked Answered
C

1

6

Has anyone been able to get unit test coverage of both JMockit and Powermock unit tests working in JaCoCo from Maven build?

I have an existing test set of Powermock unit tests, that I would like to gradually migrate to JMockit. But I need to be able to see test coverage of all unit tests in one report, preferably in Sonar.

I did get JMockit and Powermock tests running together with Surefire / JaCoCo by putting JaCoCo into "offline" mode (otherwise I had a problem where one of the agents was not being terminated at the end of the tests and then mvn clean could not delete the generated target\surefire\surefirebooter2967126910681005991.jar on the next run). But no coverage was generate for the JMockit tests.

Please post some excerpts from your pom if you have this working.

This is what my pom looks like (note surefire plugin is congigured with reuseForks=false to workaround PermGen memory leak in Powermock, this is one of the main reasons to migrate to 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>
Couturier answered 25/9, 2013 at 13:11 Comment(0)
U
0

Either instrumented classes were passed after not-instrumented ones on classpath or jacoco agent.jar was not added to test classpath.

To check both posibilities check where you have instrumented classes, run mvn with -X flag and check classpath for test execution (to see order of classpath elements and if jacoco agent is on classpath.)

Underlinen answered 22/10, 2014 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.