Andrii Abramov
Andrii Abramov

Reputation: 10751

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.

Upvotes: 4

Views: 3057

Answers (2)

toongeorges
toongeorges

Reputation: 1976

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

Upvotes: 5

GhostCat
GhostCat

Reputation: 140457

I think you can tailor yourself a solution like this:

  1. You do your "coverage data collection"
  2. You export the results of that; for example into CSV data
  3. You write a tiny bit of code that does acquires the names of all classes/packages of any JAR archive
  4. Then you simply extract those rows from the CSV file that you produced using step 3.

Given your comment: it seems that you don't understand what "coverage" is doing for you. The point is: you start a JVM; that JVM runs some code. And while doing so, data is collected about which classes/methods are used, paths taken, etc.

In other words:

  • You drive the JVM to run stuff
  • The coverage framework takes notes which code paths are taken

Nothing in that process relates to specific JARs. Coverage just checks which code is running. That mapping to "where that code is coming from is a different aspect.

Upvotes: 1

Related Questions