-
Notifications
You must be signed in to change notification settings - Fork 6
Home
Welcome to the PowerMockJacocoDemo wiki!
This project demonstrates the Powermock/Jacoco issue and provides a gradle android solution.
The combination of PowerMock and Jacoco can result in fully covered classes being reported as 0% coverage.
In a nutshell (verbatim from a jacoco forum)
- source file is compiled into non-instrumented class file
- non-instrumented class file is instrumented (either pre-instrumented offline, or automatically at runtime by Java agent)
- execution of instrumented classes collected into exec file
- report decorates source files with information obtained from analysis of exec file and original non-instrumented class files
- Message "Classes ... do no match with execution data." during generation of report means that class files used for generation of report are not the same as classes prior to instrumentation.
It’s a well-known issue and more can be found at the powermock github PowerMock/Jacoco page. On the jacoco side, more information can be found at Jacoco Class Ids
- A workaround is to use alternative mocking framworks, or use Offline Instrumentation for jacoco.
- The Powermock team does provide an Offline Instrumentation sample script/project for Maven. Unfortunately, such samples are not provided for gradle.
- This project is setup to reproduce the issue and to apply the fix, thus, creating a before and after effect. The code is based off of a project from the Powermock team to demonstrate offline intrumentation with Maven. The difference is that this project is adapted to Android and gradle.
- The main files in questions are build.gradle, jacoco.gradle, and jacoco_offline.gradle.
- build.gradle is referencing either jacoco.gradle or jacoco_offline.gradle depending what type of instrumentation desired.
- While jacoco_offline.gradle is intuitively titled, jacoco.gradle can be seen as jacoco_on_the_fly.gradle. However, we're staying clear of such nomenclature because on-the-fly instrumentation is jacoco's default behavior.
This is jacoco default setup for instrumentation and will reproduce the issue
-
Ensure that file build.gradle is referencing the jacoco.gradle file.
-
Run command
gradle clean & gradle jacocoTestReport
-
gradle clean
will wipe out any previous gradle execution artifacts -
gradle jacocoTestRerport
will run test and generate jacoco report
-
Notice the two files, Configuration.java & Lamda.java that, do not have the proper execution data.
-
Consequently the two files will display as 0% coverage in the jacoco report.
This will run offline instrumentation on the same project
-
Ensure that file build.gradle is referencing the jacoco_offline.gradle file.
-
Run command
gradle clean & gradle createOfflineTestCoverageReport & gradle jacocoTestReport
-
gradle clean
will wipe out any previous gradle execution artifacts -
gradle createOfflineTestCoverageReport
will create offline instrumentation, change order of classpath, generate .exec file -
gradle jacocoTestReport
will run test and generate jacoco report based on previously generated .exec file -
Alternatively:
gradle jacocoTestReport -x test
can be used to skip over some steps possibly covered bygradle createOfflineTestCoverageReport
-
Notice there are no alerts of execution data mismatch
-
Notice the increase and accurate coverage for files Configuration.java and Lamda.java in the jacoco html report
- It is possible to run just two tasks:
gradle clean & gradle createOfflineTestCoverageReport
- This is possible because of a snippet of the script
html(destdir: "$buildDir.path/reports/jacocoHtml")
that is commented - That snippet of code needs to be uncommented before usage. However, keep in mind that the script currently does not account for file exclusion -- this is taken care of by jacocoTestReport task.
- https://github.com/powermock/powermock/wiki/Code-coverage-with-JaCoCo
- https://www.jacoco.org/jacoco/trunk/doc/classids.html
- https://www.jacoco.org/jacoco/trunk/doc/offline.html
- https://github.com/powermock/powermock-examples-maven/tree/master/jacoco-offline
- https://automated-testing.info/t/jacoco-offline-instrumentations-for-android-gradle/20121
- https://stackoverflow.com/questions/41370815/jacoco-offline-instrumentation-gradle-script/42238982#42238982
- https://groups.google.com/forum/#!msg/jacoco/5IqM4AibmT8/-x5w4kU9BAAJ