1、下载 jacoco
官网地址:EclEmma - JaCoCo Java Code Coverage Library
2、拷贝 jar 包
下载好后,找到这两个文件,然后找到被测项目
3、启动 jacocoagent,监控被测项目
java -javaagent:jacocoagent.jar=includes=*,output=tcpserver,port=6300,address=localhost,append=true -jar jacoco-demo-1.0.0.jar
#jacoco-demo-1.0.0.jar为被测项目jar包
$ java -javaagent:jacocoagent.jar=includes=*,output=tcpserver,port=6300,address=localhost,append=true -jar jacoco-demo-1.0.0.jar
16:23:53,451 |-INFO in ch.qos.logback.classic.LoggerContext[default] - This is logback-classic version 1.4.5
16:23:53,467 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
16:23:53,483 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/E:/soft/jacoco/jacoco-0.8.10/lib/jacoco-demo-1.0.0.jar!/BOOT-INF/classes!/logback.xml]
16:23:53,499 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@4808bc9b - URL [jar:file:/E:/soft/jacoco/jacoco-0.8.10/lib/jacoco-demo-1.0.0.jar!/BOOT-INF/classes!/logback.xml] is not of type file
16:23:53,658 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - Processing appender named [stdout]
16:23:53,658 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
16:23:53,674 |-INFO in ch.qos.logback.core.model.processor.ImplicitModelHandler - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
16:23:53,722 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - Processing appender named [ErrorFile]
16:23:53,722 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
16:23:53,738 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1893960929 - No compression will be used
16:23:53,738 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1893960929 - Will use the pattern c:/logs/error/error-%d{yyyy-MM-dd}.%i.log for the active file
16:23:53,770 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2ca923bb - The date pattern is 'yyyy-MM-dd' from file name pattern 'c:/logs/error/error-%d{yyyy-MM-dd}.%i.log'.
16:23:53,770 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2ca923bb - Roll-over at midnight.
16:23:53,786 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2ca923bb - Setting initial period to Sun Aug 13 16:11:32 CST 2023
16:23:53,786 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2ca923bb - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
16:23:53,786 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2ca923bb - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
16:23:53,794 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - This appender no longer admits a layout as a sub-component, set an encoder instead.
16:23:53,794 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
16:23:53,794 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
16:23:53,794 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - Active log file name: c:/logs/error/error.log
16:23:53,794 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - File property is set to [c:/logs/error/error.log]
16:23:53,802 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - Processing appender named [AllFile]
16:23:53,802 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
16:23:53,802 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@333392524 - No compression will be used
16:23:53,802 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@333392524 - Will use the pattern c:/logs/all/all-%d{yyyy-MM-dd}.%i.log for the active file
16:23:53,802 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ebea008 - The date pattern is 'yyyy-MM-dd' from file name pattern 'c:/logs/all/all-%d{yyyy-MM-dd}.%i.log'.
16:23:53,802 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ebea008 - Roll-over at midnight.
16:23:53,802 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ebea008 - Setting initial period to Sun Aug 13 16:22:01 CST 2023
16:23:53,802 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ebea008 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
16:23:53,802 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ebea008 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
16:23:53,802 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - This appender no longer admits a layout as a sub-component, set an encoder instead.
16:23:53,802 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
16:23:53,802 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
16:23:53,802 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - Active log file name: c:/logs/all/all.log
16:23:53,802 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - File property is set to [c:/logs/all/all.log]
16:23:53,802 |-INFO in ch.qos.logback.classic.model.processor.RootLoggerModelHandler - Setting level of ROOT logger to INFO
16:23:53,802 |-INFO in ch.qos.logback.core.model.processor.AppenderRefModelHandler - Attaching appender named [stdout] to Logger[ROOT]
16:23:53,802 |-INFO in ch.qos.logback.core.model.processor.AppenderRefModelHandler - Attaching appender named [ErrorFile] to Logger[ROOT]
16:23:53,802 |-INFO in ch.qos.logback.core.model.processor.AppenderRefModelHandler - Attaching appender named [AllFile] to Logger[ROOT]
16:23:53,802 |-INFO in ch.qos.logback.core.model.processor.DefaultProcessor@72d6b3ba - End of configuration.
16:23:53,802 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@1787f2a0 - Registering current configuration as safe fallback point
16:23:54,405 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@64030b91 - URL [jar:file:/E:/soft/jacoco/jacoco-0.8.10/lib/jacoco-demo-1.0.0.jar!/BOOT-INF/classes!/logback.xml] is not of type file
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - Processing appender named [stdout]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.ImplicitModelHandler - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - Processing appender named [ErrorFile]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
16:23:54,422 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@540206885 - No compression will be used
16:23:54,422 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@540206885 - Will use the pattern c:/logs/error/error-%d{yyyy-MM-dd}.%i.log for the active file
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4d23015c - The date pattern is 'yyyy-MM-dd' from file name pattern 'c:/logs/error/error-%d{yyyy-MM-dd}.%i.log'.
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4d23015c - Roll-over at midnight.
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4d23015c - Setting initial period to Sun Aug 13 16:11:32 CST 2023
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4d23015c - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4d23015c - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - This appender no longer admits a layout as a sub-component, set an encoder instead.
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - Active log file name: c:/logs/error/error.log
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - File property is set to [c:/logs/error/error.log]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - Processing appender named [AllFile]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
16:23:54,422 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@943659381 - No compression will be used
16:23:54,422 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@943659381 - Will use the pattern c:/logs/all/all-%d{yyyy-MM-dd}.%i.log for the active file
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@441cc260 - The date pattern is 'yyyy-MM-dd' from file name pattern 'c:/logs/all/all-%d{yyyy-MM-dd}.%i.log'.
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@441cc260 - Roll-over at midnight.
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@441cc260 - Setting initial period to Sun Aug 13 16:22:01 CST 2023
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@441cc260 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@441cc260 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - This appender no longer admits a layout as a sub-component, set an encoder instead.
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - Active log file name: c:/logs/all/all.log
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - File property is set to [c:/logs/all/all.log]
16:23:54,422 |-INFO in ch.qos.logback.classic.model.processor.RootLoggerModelHandler - Setting level of ROOT logger to INFO
16:23:54,422 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@73a00e09 - Propagating INFO level on Logger[ROOT] onto the JUL framework
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderRefModelHandler - Attaching appender named [stdout] to Logger[ROOT]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderRefModelHandler - Attaching appender named [ErrorFile] to Logger[ROOT]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderRefModelHandler - Attaching appender named [AllFile] to Logger[ROOT]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.DefaultProcessor@26dcd8c0 - End of configuration.
16:23:54,422 |-INFO in org.springframework.boot.logging.logback.SpringBootJoranConfigurator@66e889df - Registering current configuration as safe fallback point
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.2)
[INFO ] 2023-08-13 16:23:54.557 [main] com.ybw.JacocoDemoApplication - Starting JacocoDemoApplication using Java 17.0.4.1 with PID 11912 (E:\soft\jacoco\jacoco-0.8.10\lib\jacoco-demo-1.0.0.jar started by 魏先生 in E:\soft\jacoco\jacoco-0.8.10\lib)
[INFO ] 2023-08-13 16:23:54.557 [main] com.ybw.JacocoDemoApplication - No active profile set, falling back to 1 default profile: "default"
[INFO ] 2023-08-13 16:23:55.593 [main] o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
[INFO ] 2023-08-13 16:23:55.597 [main] o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
[INFO ] 2023-08-13 16:23:55.597 [main] o.a.catalina.core.StandardService - Starting service [Tomcat]
[INFO ] 2023-08-13 16:23:55.597 [main] o.a.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/10.1.5]
[INFO ] 2023-08-13 16:23:55.673 [main] o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
[INFO ] 2023-08-13 16:23:55.688 [main] o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 1042 ms
[INFO ] 2023-08-13 16:23:56.038 [main] o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
[INFO ] 2023-08-13 16:23:56.054 [main] o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
[INFO ] 2023-08-13 16:23:56.054 [main] com.ybw.JacocoDemoApplication - Started JacocoDemoApplication in 2.115 seconds (process running for 2.942)
参数描述
- includes=*:这个代表了,启动时需要进行字节码插桩的包过滤,*代表所有的class文件加载都需要进行插桩。
- 你可以写成: includes=com.test.service.*;这个参数我们可以用来做maven多模块的覆盖率,比如我们只想查看service服务层的覆盖率,我们可以通过设置包路径的方式进行只统计当前包的覆盖率。
- output=tcpserver:output主要四个参数
-
file: At VM termination execution data is written to the file specified in the destfile attribute.(当jvm停止掉的时候产出dump文件,即服务挂了产出dump文件)
-
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.(常用模式,将jacocoaget作为服务,每次通过cli包进行dump命令去获取dump包)
-
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.(将jacocoagent做为客户端,向指定ip和端口的服务推送dump信息)
-
none: Do not produce any output.(不产出任何dump,dump个寂寞,忽略)
-
-
port=6300:这是jacoco开启的tcpserver的端口,请注意这个端口不能被占用。
-
address=localhost:这是对外开发的tcpserver的访问地址。
-
可以配置127.0.0.1,也可以配置为实际访问ip配置为127.0.0.1的时候,dump数据只能在这台服务器上进行dump,就不能通过远程方式dump数据。配置为实际的ip地址的时候,就可以在任意一台机器上(前提是ip要通,不通都白瞎),通过ant xml或者api方式dump数据。
-
举个栗子:我如上配置了192.168.110.1:2014作为jacoco的tcpserver启动服务,
那我可以在任意一台机器上进行数据的dump,比如在我本机windows上用api或者xml方式调用dump。如果我配置了127.0.0.1:2014作为启动服务器,那么我只能在这台测试机上进行dump,其他的机器都无法连接到这个tcpserver进行dump -
如果不知道本机ip地址,可以使用0.0.0.0,这样ip地址会绑定主机ip。
-
-
append=true:是执行数据文件已经存在,则覆盖数据将附加到现有文件。
页面或接口调用
调用接口http://localhost:8080/user/getUser?id=1
4、cli 包 dump 生成 exec 文件(注意一定要测试完毕之后)
$ java -jar jacococli.jar dump --address localhost --port 6300 --destfile jacoco-demo.exec
[INFO] Connecting to localhost/127.0.0.1:6300.
[INFO] Writing execution data to E:\gitcode\mygit\learn\jacoco\jacoco-demo\target\jacoco-demo.exec.
-
--address localhost --port 6300 指向jacocoagent启动IP和端口
-
jacoco-demo.exec 为生成exec文件名
5、生成report
$ java -jar jacococli.jar report jacoco-demo.exec --classfiles E:/gitcode/mygit/learn/jacoco/jacoco-demo/target/classes --sourcefiles E:/gitcode/mygit/learn/jacoco/jacoco-demo/src/main/java --html html-report --xml report.xml --encoding=utf-8
[INFO] Loading execution data file E:\gitcode\mygit\learn\jacoco\jacoco-demo\target\jacoco-demo.exec.
[INFO] Analyzing 4 classes.
- --sourcefiles 和 --classfiles 为本地被测项目源码和字节码路径。
- --html 为生成报告目录。
6、查看覆盖率报告
表格
- Element:元素;
- Missed Instructions Cov:未覆盖字节码行数;
- Missed Branches Cov:未覆盖的分支数;
- Cxty:圈复杂度;
- Lines:行;
- Methods:方法;
- Classes:类;
覆盖率标识
- 条件覆盖:红钻:表示未覆盖;黄钻:表示部分覆盖;绿钻:表示全部覆盖;
- 行覆盖:全覆盖(绿色),未覆盖(红色),半覆盖(黄色),无视(白色)
只调用http://localhost:8080/user/getUser?id=1,效果如下
调用如下接口
http://localhost:8080/user/getUser?id=1、http://localhost:8080/user/getUser?id=2、http://localhost:8080/user/getUser?id=3、http://localhost:8080/user/getUser?id=5
效果如下,覆盖率为100%:
7、单元测试
上面都是功能测试代码覆盖率,jacoco单元测试文章可以看:
idea 测试覆盖率_idea单元测试覆盖率_xixingzhe2的博客-CSDN博客
8、demo代码
share: 分享仓库 - Gitee.com