Powermock and JMockit unit test coverage

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

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

I got the JMockit and Powermock tests together with Surefire / JaCoCo, putting JaCoCo in the "offline" mode (otherwise I had a problem when one of the agents was not completed at the end of the tests, and then mvn clean could not remove the generated target \ surefire \ surefirebooter2967126910681005991.jar in the next run). But for JMockit tests no coverage was created.

Please post some excerpts from your help if this works for you.

This is what my pom looks like (note that the surefire plugin is configured with reuseForks = false to bypass the PermGen memory leak in Powermock, this is one of the main reasons for switching 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> 
+6
source share
1 answer

Either the tool classes were passed after the non-tool classes in the classpath, or the jacoco agent.jar was not added to the test path.

To test both possibilities, check where you have tool classes, run mvn with the -X flag and check the class path for the test to run (to see the order of the classpath elements and if the jacoco agent is in the class path.)

0
source

Source: https://habr.com/ru/post/954602/


All Articles