1

FIXED - Sorted it! Turns out, when you're adding package declarations to your file, you shouldn't add in a full path (ie src/main/java/com/company/app). Instead you should just package by whatever directories you have within app for example. So in a util directory, you can literally just have it as package util. Then Jacoco properly read the source files and calculated coverage


I've been having problems using Jacoco to generate good code coverage reports for a little project I've written.

Mostly following the example, I've read here https://www.baeldung.com/jacoco - I've configured my pom to have the prepare-agent goal and the report goal. This all seems to work in that I'm generating a site/jacoco directory with html files for different classes and so on.

However when I generate the report and look at index.html within jacoco, I get the error source file x was not found during generation of report. This is for all my files.

Link to error picture

Interestingly, when I tried adding a file called Foo.java in the root level of my project (com/philsfakecompany/chessapp) without any package declarations or management, and then ran the report - Jacoco linked it correctly, generated the source file, and calculated code coverage. This makes me suspect I'm doing something wrong with my packaging structure/directory structure.

In a GameManager.java files, I have given it the package com.philsfakecompany.chessapp, which just reflects the project directory structure - and I see that same structure maintained in the generated binaries in the target folder. Within jacoco, it comes out as a few files withinsite/jacoco/com.philsfakecompany.chessapp.

Anyone know what I'm doing wrong here? I assumed you were supposed to add package declarations to all files and that they essentially reflect the directory structure. I could get around this problem by removing any package structure and having all my files in root - but that feels horrible.

I've had a poke around other stack overflow posts -and the most relevant I found was JaCoCo Not Generating Coverage Reports based on Source File - Method names not clickable. However it doesn't solve my problem. I have line numbers in the generated pages- so I know its not a problem with classes not being compiled with debug information. I suspect it might be a problem with packages, but I think I'm following the example they set in that post

I'll add my POM below as well in case any errors are coming from there.

<plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.6</version>
        <executions>
          <execution>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <!-- attached to Maven test phase -->
          <execution>
            <id>report</id>
            <phase>test</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
        </configuration>
      </plugin>

If anyone has any ideas, I'd be super grateful!

  • Sorted it! Turns out, when you're adding package declarations to your file, you shouldn't add in a full path (ie src/main/java/com/company/app). Instead you should just package by whatever directories you have within `app` for example. So in a util directory, you can literally just have it as package util. – ConfusedDev1234325 Apr 21 '21 at 15:21
  • 3
    What exactly do you mean by `"when you're adding package declarations to your file"` and `"you should just package by whatever directories you have within app"`?? What configuration did you use to solve the problem? – Mr-IDE May 24 '21 at 05:43

0 Answers0