jacoco: For report generation the same class files must be used as at runtime

I ran into this when trying to run the jacocoTestReport task separate from my test task—not different Java versions, just stale classes.

One workaround is to make sure you get a fresh build and a fresh test run:

$ gradle clean && gradle test && gradle jacocoTestReport

Another is to create a composite task that runs test and jacocoTestReport in order. In your build.gradle:

task coverage {
  dependsOn 'test'
  dependsOn 'jacocoTestReport'
  tasks.findByName('jacocoTestReport').mustRunAfter('test')
}

I'm new to JaCoCo, and I feel like this should be easier, but this worked for me.


If you are using IntelliJ + Maven, try this

Go to File --> Invalidate Caches/Restart


This happens when you are using a Java version X to compile the classes and running the tests (jacoco) but running the jacocoTestReport task (in Gradle), you are using another Java version aka Java Y.

Are you setting a different JAVA version or using a different Gradle (which is using different JAVA)? You may be getting a partial coverage as when you see this warning/error message about execution data for class xxx/yyy/zzz does not match, that means it'll reflect 0% coverage.

To fix the following issue:

[ant:jacocoReport] Classes in bundle 'app' do no match with execution data. For report generation the same class files must be used as at runtime.
[ant:jacocoReport] Execution data for class com/my/app/MyClass does not match.

Make sure that:

  1. You JAVA version is SAME while running "gradle clean build" or "gradle integrationTest" or "gradle someKindOfnonUnitTest" (behind external container like Tomcat etc) and for generating the coverage report using jacoco using jacocoTestReport task.

OR if the above doesn't help the,

  1. While running "gradle jacocoTestReport" (assuming you have the valid .exec files for your Unit / NonUnit tests in the workspace), pass some command line switches like shown below.

For ex:

gradle clean build 
gradle ...some..nonUnitTestTask

then

gradle jacocoTestReport -x test -x testClasses -x compileJava -x classes