I had an interesting problem with Jacoco displaying 0% coverage for a class under unit test if @PrepareForTest used.
In short, my unit test uses a combination of @RunWith(PowerMockRunner.class) and @PrepareForTest(SomeStaticMethodClass.class) to run unit tests that require mocking static methods. You can read these two articles that I wrote about mocking static methods ( 1 , 2 ). Bullying static methods works fine. The bad side effect is that when Jacoco runs unit tests using the @PrepareForTest annotation, any unit of the class being tested is ignored, and the Jacoco report for this class shows a 0% coverage. I highlighted the problem specifically for @PrepareForTest annotation. I did this by creating a simple unit test POJO, got 100% coverage of this POJO, and then added @PrepareForTest to the unit test in the annotation. After adding to annotations, Jacoco's messages show that POJO coverage is 0%.
Anyone have any thoughts on this?
Here is the technical information:
java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode) Apache Maven 3.0.4 (r1232337; 2012-01-17 02:44:56-0600) <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.4.201502262128</version> . . . </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> . . . </plugin> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.5.6</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-easymock</artifactId> <version>1.5.6</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.5.6</version> <scope>test</scope> </dependency> <dependency> <groupId>org.easymock</groupId> <artifactId>easymock</artifactId> <version>3.2</version> <scope>test</scope> </dependency> <reporting> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.7</version> </plugin> </plugins> </reporting>
java unit-testing powermock mocking jacoco
Michael remijan
source share