Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage of built JAR

Is there an ability to generate code coverage reports for a JAR file belonging to my class path?

The jar is already compiled and added as a library to Gradle project.

The test code for jar is stored in the project.

like image 224
Andrii Abramov Avatar asked Oct 19 '25 10:10

Andrii Abramov


1 Answers

Is there an ability to generate code coverage reports for added to classpath jar?

There should be. You are not telling which code coverage tool you are using though. From the tags in the question it appears you are using JaCoCo.

Code coverage typically goes through 3 phases:

  1. Instrumentation: you run the code coverage tool against the compiled classes, which creates an instrumented version of the classes. Conceptually, the instrumented classes have code injected at every line that increments a counter. This is how is being tracked which line has been visited.
  2. Code Execution of instrumented classes: the code is run and the counters collect which lines have been accessed.
  3. Reporting: the data collected by the counters is read and used to generate a code coverage report. At this stage you would need the source code so the code coverage tool can match the code coverage result with actual sources.

In your scenario, what you could do is to unzip the JAR for which you want to have code coverage, instrument the classes within and zip these classes to another JAR. Replace the original JAR with the JAR with instrumented classes.

This is a non standard workflow and may not be easily configured in Ant/Maven, but you can do it programmatically, see the API and API examples

like image 94
toongeorges Avatar answered Oct 22 '25 01:10

toongeorges



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!