'How do I generate a JaCoCo report for a JAR file?

We are trying to report code coverage of tests against a pre-packaged JAR file using JaCoCo. To do this we start the JAR file using java -jar with the additional argument:

-javaagent:${project.basedir}/tools/jacocoagent.jar=output=tcpserver,port=${jacoco.port}

JaCoCo's Maven plugin is configured to then dump the execution file and report the results:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.0</version>
    <executions>
        <execution>
            <id>dumpData</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>dump</goal>
            </goals>
            <configuration>
                <address>localhost</address>
                <port>${jacoco.port}</port>
                <destFile>target/jacoco.exec</destFile>
            </configuration>
        </execution>
        <execution>
            <id>report</id>
            <phase>verify</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <dataFile>target/jacoco.exec</dataFile>
            </configuration>
        </execution>
    </executions>
</plugin>

This works to the point that a jacoco.exec file is generated which has a non-zero size (~350kB).

But the report shows no coverage at all. Following the "Sessions" link I can see the classes in the JAR listed, but the report's home page shows this: JaCoCo Report

Based on the logs we have the code seems to be exercised. Is there a step missing in the JaCoCo setup or should this work?



Solution 1:[1]

Here are the steps to achieve this:

  1. Run the java application in a separate JVM with -javaagent parameter passed.

  2. Run your tests against the application(like calling an endpoint on the web server, etc.)

  3. Run the dump and report goals to generate the report.

You can check out my article on the this topic.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Code Journal