Getting code coverage of my application using JaCoCo Java agent on Tomcat
Asked Answered
R

3

17

I want to measure the code coverage of integration tests using the JaCoCo and Sonar tools.

For that, I start my Tomcat 5.5 configured with the JaCoCo agent in order to get the dump file from JaCoCo.

Thus, I set the JAVA_OPTS for that:

set JAVA_OPTS=-Xrs -XX:MaxPermSize=256m -XX:PermSize=256m -XX:NewRatio=3 -Xms512m -Xmx1024m -XX:+UseParallelGC -javaagent:C:\dev\servers\jacoco-agent.jar=destfile=C:\dev\servers\jacoco.exec,append=true,includes=my.application.*

When I start Tomcat, the C:\dev\servers\jacoco.exec file is generated, but no data is filled.

Is there something I forgot in the configuration of my server?

Regards.

Roussel answered 28/7, 2011 at 9:24 Comment(1)
A
9

As far as I remember - file would be populated during shutdown of Tomcat.

Alephnull answered 2/10, 2011 at 19:48 Comment(2)
Ok, I will try that this week, and return to you.Roussel
If you don't want to shutdown your server, like we do, Cobertura has a coberturaFlush webapp that can be called using host:port/coberturaFlush/flushCobertura. Be sure to have your cobertura jar loaded by the same classloader.Astrosphere
R
13

I realize this may not have been an option 2 years ago when this question was asked, but presently you have some other options available to fetch the JaCoCo execution data without shutting down Tomcat (or any JVM instrumented with the JaCoCo java agent).

First take a look at the current documentation for the JaCoCo Java Agent: http://www.eclemma.org/jacoco/trunk/doc/agent.html

You can use the output=tcpserver option on the JaCoCo agent to have the Java agent listen for commands. You can set address=* to have the tcpserver listen on all interfaces, and you can set the port=6300 argument to choose the port where the tcpserver should listen.

Through the tcpserver the JaCoCo java agent can be instructed to send you the data whenever you ask for it.

If your JVM is currently exposing JMX you have another option which you can utilize without opening additional ports. By setting the jmx=true option the JaCoCo java agent exposes an MBean which you can interact with.

If you are using maven you can take a look at the plugin I recently wrote in order to gather JaCoCo data from remote JVM's while running. The project for the plugin is located at:
https://github.com/mattcj/jacocotogo

Rigdon answered 19/11, 2013 at 21:0 Comment(1)
jacocotogo is a useful plugin but do you know how to set up so it will pick coverage? I have a plugin that stops application server at same phase post-integration-test so it is skipped.Osugi
F
10

Besides the maven solution, you can also consider https://www.eclemma.org/jacoco/trunk/doc/cli.html

Basically, you start your service on the remote machine with the javaagent option like (you can change the port number and omit includes if you want to have coverage for all of the classes):

-javaagent:/tmp/jacocoagent.jar=port=36320,destfile=jacoco-it.exec,output=tcpserver,includes=a.b.c.d.*”

Then connect to the remote machine by providing remote host address or open a tunnel to the remote machine. The following example assumes I have set up a port forwarding between local host's 36320 and remote host's 36320

java -jar jacococli.jar dump --port 36320 --destfile /tmp/jacoco-it.exec

If you have multiple .exec files, you need to merge them:

java -jar jacococli.jar merge /tmp/jacoco-it-1.exec /tmp/jacoco-it-2.exec --destfile /tmp/merge

Then generate the html report (path1 can be a path to the jar file or the class files folder)

java -jar jacococli.jar report /tmp/jacoco-it.exec --classfiles path1 --sourcefiles path2 --html /tmp/report

Flotation answered 13/11, 2018 at 18:30 Comment(0)
A
9

As far as I remember - file would be populated during shutdown of Tomcat.

Alephnull answered 2/10, 2011 at 19:48 Comment(2)
Ok, I will try that this week, and return to you.Roussel
If you don't want to shutdown your server, like we do, Cobertura has a coberturaFlush webapp that can be called using host:port/coberturaFlush/flushCobertura. Be sure to have your cobertura jar loaded by the same classloader.Astrosphere

© 2022 - 2024 — McMap. All rights reserved.