11

I've run into an interesting problem with Jacoco reporting 0% coverage for a class under unit test if @PrepareForTest is used.

Long story short, my unit test is using a combination of @RunWith(PowerMockRunner.class) and @PrepareForTest(SomeStaticMethodClass.class) to perform unit tests which require the mocking of static methods. You can read these two articles I wrote on mocking static methods (1, 2). The mocking of the static methods works great. The bad side effect is that when Jacoco runs the unit tests with a @PrepareForTest annotation, any class unit tested is ignored and the Jacoco report for that class shows 0% coverage. I've isolated the problem specifically to the @PrepareForTest annotation. I did this by creating a simple unit test of a POJO, got 100% coverage on that POJO, then added in the @PrepareForTest annotation on the unit test. After adding in that annotation the Jacoco reports shows that POJO coverage at 0%.

So anyone have any thoughts on this one?

Here is some 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>
Eric
  • 6,563
  • 5
  • 42
  • 66
Michael Remijan
  • 687
  • 7
  • 22

0 Answers0