11

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?

Betlista
  • 10,327
  • 13
  • 69
  • 110
Peter Becker
  • 8,795
  • 7
  • 41
  • 64
  • 1
    I'm not sure what you want to achieve with that `-javaagent`. It should get coverage from tests - did you read http://www.baeldung.com/jacoco ? So during the Maven build, you are running tests and based on those tests coverage is measured. This is typical usage as I know it... – Betlista Feb 19 '18 at 07:44
  • 4
    I'm doing black-box testing against an already built JAR file. We are starting up a Spring Boot application and hit it with HTTP requests, I'm trying to measure the code coverage of that. Not a typical usage, but as far as I understand the documentation it should be doable. Maybe I'm wrong. – Peter Becker Feb 19 '18 at 23:08
  • I have no experience with that, maybe you can look at https://carlosbecker.com/posts/production-code-coverage-jacoco/ ;-) – Betlista Feb 19 '18 at 23:15
  • Running the report from the CLI tool, pointing the "--classfiles" to the JAR, I get an exeption "Error while analyzing ....app.jar@BOOT-INF/lib/aopalliance-1.0.jar@org/aopalliance/aop/AspectException.class" - it looks like it can't handle the way Spring Boot packages classes. Maybe that's why my report is empty. – Peter Becker Feb 21 '18 at 05:18
  • any solution on that? – dicle Mar 28 '19 at 13:34
  • Sorry, I should have documented the answer. Now I have moved on, and I don't remember what we did. We had coverage reports in the end, but I don't even remember if we stuck with JaCoCo or not. – Peter Becker Mar 29 '19 at 15:35

1 Answers1

0

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.

Code Journal
  • 161
  • 4