Java应用程序的测试覆盖率之设计与实现(二)-- jacoco agent

说在前面的话

要想获得测试覆盖率报告,第一步要做的是,采集覆盖率数据,并输入到tcp。

而本文便是介绍一种java应用程序部署下的推荐方式。

作为一种通用方案,首先不想对应用程序有所侵入,其次运维和管理方便。

正好,jacoco agent就是类似于pinpoint agent一样,都使用了javaagent技术。

在灰度部署、压力测试等都许多场景下,都适用于javaagent技术。

一、jacoco agent

java agent技术,对jar应用程序没有入侵。

其作用是采集覆盖率数据,建议采用tcpserver的方式往外部输出。

安装简单,启动脚本增加以下参数:

bash 复制代码
basepath=/opt
# 本机IP
LOCAL_IP=192.168.10.128
# 服务名称
service=

# java包名是com.tech.*
java -javaagent:${basepath}/jacocoagent.jar=includes=com.tech.*,output=tcpserver,address=${LOCAL_IP},port=9322,classdumpdir=${basepath}/${service}/classdumpdir/classes/  -jar xxx.jar

二、参数说明

1、includes

默认是* (all classes) , 填写的是项目的class文件所在包package,多个之间使用冒号隔开。

2、output

  • file: At VM termination execution data is written to the file specified in the destfile attribute.
  • tcpserver: The agent listens for incoming connections on the TCP port specified by the address and port attribute. Execution data is written to this TCP connection.
  • tcpclient: At startup the agent connects to the TCP port specified by the address and port attribute. Execution data is written to this TCP connection.
  • none: Do not produce any output.

填写的是tcpserver,支持远程读取覆盖率数据。而file是保存于本地目录。

3、host

tcp server地址

4、port

tcp server端口

5、classdumpdir

class文件所在目录

下图为本地启动的测试结果。

6、append

是否追加,默认是true,设置为false时,每次重新生成。

三、总结

bash 复制代码
> ss -anlp |grep 9122
tcp  LISTEN  0  1  [::ffff:192.168.80.148]:9122  [::]:*  users:(("java",pid=18901,fd=10))

接下来,我们就可以远程连接tcpserver,dump出覆盖率数据了。

相关推荐
.格子衫.3 小时前
Spring Boot 原理篇
java·spring boot·后端
多云几多3 小时前
Yudao单体项目 springboot Admin安全验证开启
java·spring boot·spring·springbootadmin
另寻沧海5 小时前
测试中的 AAA 模式与 Given–When–Then 模式详解
c++·单元测试·测试覆盖率
Jabes.yang6 小时前
Java求职面试实战:从Spring Boot到微服务架构的技术探讨
java·数据库·spring boot·微服务·面试·消息队列·互联网大厂
聪明的笨猪猪6 小时前
Java Redis “高可用 — 主从复制”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
兮动人6 小时前
Spring Bean耗时分析工具
java·后端·spring·bean耗时分析工具
MESSIR226 小时前
Spring IOC(控制反转)中常用注解
java·spring
摇滚侠6 小时前
Spring Boot 3零基础教程,Demo小结,笔记04
java·spring boot·笔记
笨手笨脚の7 小时前
设计模式-迭代器模式
java·设计模式·迭代器模式·行为型设计模式
spencer_tseng8 小时前
Eclipse 4.7 ADT (Android Development Tools For Eclipse)
android·java·eclipse