I recently used EclEmma plugin in eclipse to find code coverage of JUnits in my application and it worked great.
In the next step, I want to be able to find code coverage for end-to-end tests / functional tests which are called on our application from outside(they reside on a separate server).
Basically, we package our application as a jar (we use maven) and deploy it on a server. And we can trigger the functional tests at this location.
Is there a way to find code coverage in this case?
My understanding of how code coverage tool works (in raw language) is that it loads the classes and generates a report based on the part of code that has been hit. So as per this understanding, I don't need to have access to the test-code. I only need to somehow plug the code coverage tool into my application jar and whenever any code in this jar is called, report will be updated. Is my understanding correct?
NOTE:
I am open to use other code coverage tool if this is possible with other tools.
You can run your code on a server, instrumented at runtime by the JaCoCo agent, by adding the agent to the Java command line. For example if your process is currently launched with:
java -jar myApp.jar
You can change it to
java -jar myApp.jar -javaagent:/some/path/jacocoagent.jar
By default this will write coverage data to the file jacoco.exec
when the VM terminates, but you can override this with options, enabling you to get coverage data over TCP/IP. See the JaCoCo Agent docs.
You can format this into a report (e.g. HTML) using:
java -jar jacococli.jar report jacoco.exec [options]
See the JaCoCo CLI docs for options.
... or you can use EclEmma to analyse the output.
If there are genuine reasons you can't use the agent, as a last resort you can statically convert your class files into instrumented class files, using the instrument
command in jacocococli
. See the JaCoCo 'Offline Instrumentation' documentation for information about this.
Directly invoking the agent and using jacococli.jar
are the most basic ways of using JaCoCo. Since you are using Maven, you can get many of the same effects using the JaCoCo Maven plugin.
This is how i did it with maven in a jenkins pipeline
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent test -Dmaven.test.failure.ignore=true
This will create a target/jacoco.exec
that has the code coverage data.
Obviously we cannot interpret the output, but tools and plugins like SonarQube can do that.(In my case i used sonarqube)
You can however use below to generate into html/csv format which will be located in target/site/jacoco/index.html
, target/site/jacoco/jacoco.csv
mvn org.jacoco:jacoco-maven-plugin:report
OR
java -jar jacococli.jar report jacoco.exec [options]
Alternatively, you can refer to this tutorial which did all this in the pom.xml