Jacoco: Find code coverage for external tests
I had the same issue. This is how I fixed it locally. By adding the jacoco agent in the vm args. In addition, after jacoco version 6.1, the exec file is created at the beginning (blank) and then later populated after the server is gracefully shutdown which apparently eclipse doesn't do. So we get an empty .exec file. The key is to add output=tcpserver and then import the code coverage.
-javaagent:C:\Users\JohnDoe\Downloads\jacoco-0.8.5\lib\jacocoagent.jar=output=tcpserver.
Import the results into Eclipse. This is done from File -> Import -> Coverage Session -> select Agent address radio button but leave defaults -> enter some name and select code under test. Default should be 127.0.0.1 with port 6300.
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
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.