Getting code coverage of my application using JaCo

2019-02-12 17:13发布

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.

3条回答
The star\"
2楼-- · 2019-02-12 17:48

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

查看更多
劫难
3楼-- · 2019-02-12 18:03

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

查看更多
太酷不给撩
4楼-- · 2019-02-12 18:04

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

查看更多
登录 后发表回答