Jacoco Java Code Coverage Library Jacoco is an open source coverage tool. Jacoco can be embedded in ant, Maven, and provide Eclemma eclipse Plug-ins, or you can use Java Agent technology to monitor Java programs. Many Third-party tools provide an integration of jacoco, such as Sonar, Jenkins, and idea.
Official Address http://www.eclemma.org/jacoco/
Java Counters Jacoco includes a variety of scale coverage counters, including instruction levels (INSTRUCTIONS,C0 coverage), branching (BRANCHES,C1 coverage), cyclomatic complexity (cyclomatic complexity), rows (Lines ), Method (Non-abstract methods), Class (classes).
As shown below, the green is covered with branches, the yellow is covered by branches, and the branch is not executed in red.
Integration The Jacoco team provides support for some of the following integration tools
Java API
Command Line
Apache Ant
Apache Maven
Eclipse Ecldmma Plugin
Jacoco-maven-plugin
Jacoco overall provides a relatively complete document, but the MAVEN project needs to support Jacoco, configuration is more complex, than cobertura convenient. In order to be able to run through the plugin, research on a number of articles, where the ' creating Code Coverage Reports for the units and integration Tests with the Jacoco Maven Plugin ' Help the most.
The specific MAVEN configuration is as follows, executed MVN test.
<!--Jacoco plugin--> <plugin> <groupId>org.jacoco</groupId> <artifactid>jacoco- maven-plugin</artifactid> <version>0.6.4.201312101107</version> <executions> <exe
Cution> <!--in the Initialize phase of MAVEN, the Jacoco Runtime agent is passed as a parameter of the VM to the test program for monitoring calls in the JVM. --> <id>default-prepare-agent</id> <goals> <goal>prepare-agent</g oal> </goals> <configuration> <destFile> ${project.build . directory}/coverage-reports/jacoco.exec </destFile> <propertyName>surefireArgLine< /propertyname> </configuration> </execution> <!--in the verify phase of the program, executive
Test the program.
The input for the file is set in the perpare-agent phase or the default jacoco.exec. Parameters includes and excludes can be used to select the filtered class in the list.
--> <execution> <id>default-report</id> <phase>test</phase> <goals> <goal >report</goal> </goals> <configuration> <datafile>${project.build. Directory}/coverage-reports/jacoco.exec</datafile> <outputdirectory>${project.reporting.outputdire
ctory}/jacoco</outputdirectory> </configuration> </execution> </executions>
</plugin> <!--use Maven-surefire-plugin to perform unit tests.
Assign the Surefireargline to the Argline parameter to ensure that the Jacoco agent is running while the test is executing. --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>maven-surefire-p lugin</artifactid> <version>2.16</version> <configuration> <argline>${surefi reargline}</argline> </configuration> </plugin> </plugins>
dynamic monitoring of Web project coverage Jacoco uses the Java Agent dynamic interpolation to monitor the coverage data for file execution. The Jacoco agent collects execution information, and when the JVM exits dump, there are 3 ways to output the data:
1, files, when the JVM exits, writes the execution data to the local 2,tcp Socket Server, connects the JVM as an external tool, and obtains the execution data through the Socket. 3,tcp Sockect Client, the agent connects to the TCP endpoint when the program starts, and the data output is executed through request.
The Java Agent can be started by using JVM parameters such as the following:
-JAVAAGENT:[YOURPATH/]JACOCOAGENT.JAR=[OPT1]=[VAL1],[OPT2]=[VAL2]
get dump data when shutdown JVM
Jacoco Plug-ins to obtain coverage information files through the agent, in order to ensure that plug-ins can be connected to the Jacoco agent, we must properly configure the Jacoco agent and the application to run together first. In shutdown, the dump data is obtained and the following parameters are added to the java_opts in Tomcat catalina.sh
-javaagent:[yourpath/]jacocoagent.jar=includes=com.xxx.*,output=xxx
among them, output=file/tcpserver/tcpclient:
The form of coverage data output, respectively, is file,tcpserver,tcpclient.
Corresponding Jenkins plugin see Https://wiki.jenkins-ci.org/display/JENKINS/JaCoCo+Plugin Run as client
Add the following parameters to the java_opts in the catalina.sh of Tomcat
-javaagent:[yourpath/]jacocoagent.jar=includes=com.baidu.*, output=tcpserver,port=6300,address= 220.181.111.148
This provides client access on the 6300 port of the 220.181.111.148 address, and can dump the coverage file at this time.
In this way, you do not need to stop the JVM, just request the port address provided by the test environment.
This invocation method provides APIs for generating reports, etc. on the Jacoco Web site.
Please check the details: http://blog.csdn.net/kittyboy0001/article/details/26400091