Maven - No coverage when working with JaCoCo's offline instrumentation option
Problem: When working with both JaCoCo & Sealights, no coverage is captured for our Tests
JaCoCo has two way’s of instrumenting the code for capturing coverage:
In memory - JaCoCo is added as a javaagent and performs the instrumentation in memory when the JVM comes up in Surefire
Offline - JaCoCo instruments the physical files saved on the disk which get used by the JVM run by surefire
As the second option physically changes the code, it creates a situation where what Sealights originally scanned, and what Sealights receives from the coverage do not match, and therefore no coverage it calculated
In order to capture coverage from JaCoCo for Unit Tests, there is no need to use the Offline option, and it is sometimes used by mistake instead of the In memory option.
Solution
Change you maven JaCoCo configuration to use the prepare-agent
goal instead of the instrument & restore-instrumented-classes
goals
For example:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.9-SNAPSHOT</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
...
Instead of:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.9-SNAPSHOT</version>
<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>
...