Jacoco offline instrumentation not working in Java 11 for PowerMock tests

4k Views Asked by At

I am unable to get the code coverage in Java 11 for Powermock tests using Jacoco maven plugin. My version details are as below:

Java version: jdk-11.0.2
Maven version: maven 3.5.2
Jacoco maven plugin version: 0.8.5
Powermock version: 2.0.0-beta.5

I have also added the jacoco runtime agent as we are supposed to in the classpath of all the subprojects:

<dependencies>
    <!-- Added to provide jacocoagent in classpaths for offline instrumentation builds
         Refer https://www.jacoco.org/jacoco/trunk/doc/offline.html -->
    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>org.jacoco.agent</artifactId>
        <classifier>runtime</classifier>
        <scope>test</scope>
        <version>${jacoco.plugin.version}</version>
    </dependency>
</dependencies>

These are my plugin details (Please note that these settings work absolutely fine in Java 8):

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
          <!-- Skips unit tests if the value of skipTests property is true -->
          <skipTests>${skipTests}</skipTests>
          <!-- Excludes integration tests when unit tests are run -->
          <excludes>
             <exclude>**/IT*.java</exclude>
          </excludes>
          <systemPropertyVariables>
             <jacoco-agent.destfile>${project.build.directory}/target/jacoco.exec</jacoco-agent.destfile>
          </systemPropertyVariables>
          </configuration>
    </plugin>
   <plugin>
     <groupId>org.jacoco</groupId>
     <artifactId>jacoco-maven-plugin</artifactId>
     <configuration>
       <append>true</append>
       <destFile>${user.dir}/target/jacoco.exec</destFile>
       <excludes>
         <exclude>*</exclude>
       </excludes>
       <dataFile>${user.dir}/target/jacoco.exec</dataFile>
       <outputDirectory>${user.dir}/target/coverage-reports/jacoco</outputDirectory>
       </configuration>
       <executions>
         <execution>
           <id>default-instrument</id>
           <goals>
              <goal>instrument</goal>
           </goals>
         </execution>
         <execution>
           <id>default-restore-instrumented-classes</id>
           <goals>
             <goal>restore-instrumented-classes</goal>
           </goals>
         </execution>
         <execution>
           <id>jacoco-site</id>
           <phase>package</phase>
           <goals>
             <goal>report</goal>
           </goals>
         </execution>
      </executions>
    </plugin>

Result:

[INFO] --- jacoco-maven-plugin:0.8.5:instrument (default-instrument) @ utils ---
[WARNING] The POM for org.codehaus.plexus:plexus-utils:jar:1.1 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO]


[INFO] --- jacoco-maven-plugin:0.8.5:restore-instrumented-classes (default-restore-instrumented-classes) @ utils ---
[INFO]


[INFO] --- jacoco-maven-plugin:0.8.5:report (jacoco-site) @ utils ---
[INFO] Skipping JaCoCo execution due to missing execution data file.

As you can see the jacoco.exec file is not being generated in case of offline instrumentation.

Please note that I am able to generate jacoco.exec file when I am not using offline instrumentation (i.e. using the prepare-agent goal) however it ignores the PowerMock tests.

As per this issue: https://github.com/jacoco/jacoco/issues/663

Support for Java 11 has been provided in version 0.8.3 however I am not sure if any changes were made in the offline instrumentation that is causing any issue.

Please advise if anything else is required from my end.

2

There are 2 best solutions below

0
On

Along with offline instrument, also make sure the correct version has been set to the -source and -target of the Java Compiler. This also impacts the coverage report as sometime only offline instrument settings doesn't help to get power mock test coverages.

<properties>
        ....
       <maven.compiler.source>11</maven.compiler.source>
       <maven.compiler.target>11</maven.compiler.target>
        .....
</properties>   
..
..

<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                  <source>${maven.compiler.source}</source>
                  <target>${maven.compiler.target}</target>
                    
            </plugin>
0
On

Updating the Powermock Version to 2.0.7 fixed the issue. I am now able to generate the jacoco.exec file using offline instrumentation.