1, 下载Jacoco核心包. (https://www.jacoco.org/jacoco/), 此处使用jacoco-0.8.1.
2, 添加Jboss6 VM配置:
-javaagent:{Your path}/jacoco-0.8.1/jacocoagent.jar=output=tcpserver,port=6300,address=*,includes=com.xxx.*
这里设置address=*是为了确保远程客户端能成功连接Jacoco服务器。
3, 启动Jboss6,正常情况下,会看到6300端口处于监听状态。
4, 在Jboss6中完成代码的测试工作。
5, 在测试过程中可以使用Dump命令实时查看代码覆盖测试情况。注意:如果需要重启或者关闭Jboss6,则必须先使用Dump命令将测试结果保存。
java -version
pause
java -jar jacococli.jar dump --address localhost --destfile dump/test.exec
pause
6, 使用Merge合并多个*.exec。如果在整个测试过程中产生了多个.exec文件,则需要在生成报表前将其合并。
::Merges multiple exec files into a new one.
::set path=D:\Java\jdk1.6.0_34\bin
set path=D:\Java\jdk1.8.0_131\bin
java -jar jacococli.jar merge dump/*.exec --destfile merge/all.exec
pause
7, 生成测试报表
pause
java -jar jacococli.jar report merge/all.exec --classfiles D:\IDE\content_env_jboss6\jboss-eap-6.2-Spring2.5\standalone\deployments\content-productimport.war\WEB-INF\classes --html report.html --sourcefiles Z:\git\content-productimport\src\main\java
pause
Jacoco Doc Reference (https://www.jacoco.org/jacoco/):
Java Agent
JaCoCo uses class file instrumentation to record execution coverage data. Class files are instrumented on-the-fly using a so called Java agent. This mechanism allows in-memory pre-processing of all class files during class loading independent of the application framework.
If you use the JaCoCo Ant tasks or JaCoCo Maven plug-in you don't have to care about the agent and its options directly. This is transparently handled by the them.
The JaCoCo agent collects execution information and dumps it on request or when the JVM exits. There are three different modes for execution data output:
- File System: At JVM termination execution data is written to a local file.
- TCP Socket Server: External tools can connect to the JVM and retrieve execution data over the socket connection. Optional execution data reset and execution data dump on VM exit is possible.
- TCP Socket Client: At startup the JaCoCo agent connects to a given TCP endpoint. Execution data is written to the socket connection on request. Optional execution data reset and execution data dump on VM exit is possible.
The agent jacocoagent.jar
is part of the JaCoCo distribution and includes all required dependencies. A Java agent can be activated with the following JVM option:
-javaagent:[yourpath/]jacocoagent.jar=[option1]=[value1],[option2]=[value2]
The JaCoCo agent accepts the following options:
Option | Description | Default |
destfile | Path to the output file for execution data. | jacoco.exec |
append | If set to true and the execution data file already exists, coverage data is appended to the existing file. If set to false , an existing execution data file will be replaced. | true |
includes | A list of class names that should be included in execution analysis. The list entries are separated by a colon (: ) and may use wildcard characters (* and ? ). Except for performance optimization or technical corner cases this option is normally not required. | * (all classes) |
excludes | A list of class names that should be excluded from execution analysis. The list entries are separated by a colon (: ) and may use wildcard characters (* and ? ). Except for performance optimization or technical corner cases this option is normally not required. | empty (no excluded classes) |
exclclassloader | A list of class loader names that should be excluded from execution analysis. The list entries are separated by a colon (: ) and may use wildcard characters (* and ? ). This option might be required in case of special frameworks that conflict with JaCoCo code instrumentation, in particular class loaders that do not have access to the Java runtime classes. | sun.reflect.DelegatingClassLoader |
inclbootstrapclasses | Specifies whether also classes from the bootstrap classloader should be instrumented. Use this feature with caution, it needs heavy includes/excludes tuning. | false |
inclnolocationclasses | Specifies whether also classes without a source location should be instrumented. Normally such classes are generated at runtime e.g. by mocking frameworks and are therefore excluded by default. | false |
sessionid | A session identifier that is written with the execution data. Without this parameter a random identifier is created by the agent. | auto-generated |
dumponexit | If set to true coverage data will be written on VM shutdown. The dump can only be written if either file is specified or the output is tcpserver /tcpclient and a connection is open at the time when the VM terminates. | true |
output | Output method to use for writing coverage data. Valid options are:
| file |
address | IP address or hostname to bind to when the output method is tcpserver or connect to when the output method is tcpclient . In tcpserver mode the value "* " causes the agent to accept connections on any local address. | loopback interface |
port | Port to bind to when the output method is tcpserver or connect to when the output method is tcpclient . In tcpserver mode the port must be available, which means that if multiple JaCoCo agents should run on the same machine, different ports have to be specified. | 6300 |
classdumpdir | Location relative to the working directory where all class files seen by the agent are dumped to. This can be useful for debugging purposes or in case of dynamically created classes for example when scripting engines are used. | no dumps |
jmx | If set to true the agent exposes functionality via JMX under the name org.jacoco:type=Runtime . Please see the security considerations below. | false |
Security Consideration for Remote Agent Control
The ports and connections opened in tcpserver
and tcpclient
mode and the JMX interface do not provide any authentication mechanism. If you run JaCoCo on production systems make sure that no untrusted sources have access to the TCP server port, or JaCoCo TCP clients only connect to trusted targets. Otherwise internal information of the application might be revealed or DOS attacks are possible.