Springboot 3中,使用jacoco插件生成单元测试覆盖率报告

流水线部署中,单元测试覆盖率是一项考量的基本指标.本文介绍,在springboot3项目中,如何集成jacoco插件,以便在流水线SonarQube 中展示单元测试覆盖率报告.

主要注意的问题:

1.在引用依赖的时候,jmokit的依赖要先于junit的依赖。

java 复制代码
<!-- jmockit -->
		<dependency>
			<groupId>mockit</groupId>
			<artifactId>jmockit</artifactId>
			<version>${jmockit.version}</version>
			<scope>test</scope>
		</dependency>
		<!-- junit 5-->
		<dependency>
			<groupId>org.junit.platform</groupId>
			<artifactId>junit-platform-launcher</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.vintage</groupId>
			<artifactId>junit-vintage-engine</artifactId>
			<scope>test</scope>
		</dependency>

2.surefire-maven-plugin,需要指定jmockit jar以及生成jacoco.exec位置,便于生成测试报告.

java 复制代码
<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<builder>paketobuildpacks/builder-jammy-base:latest</builder>
					</image>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
			<!-- 配置Jacoco Maven插件 -->
			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<version>${jacoco.version}</version>
				<configuration>
					<!-- jacoco生成报告时,排除和统计的文件 -->
					<excludes>
						<exclude>**/dto/**</exclude>
						<exclude>**/domain/**</exclude>
					</excludes>
					<includes>
						<include>**/service/**</include>
						<include>**/util/**</include>
					</includes>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>prepare-agent</goal>
						</goals>
					</execution>
					<execution>
						<id>report</id>
						<phase>test</phase>
						<goals>
							<goal>report</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

			<!-- 配置Surefire插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>3.0.0-M7</version> <!-- 使用支持Spring Boot 3的Surefire版本 -->
				<configuration>
					<argLine>
						<!-- jdk 9 及以后,需要指定 jmockit-->
						-javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
						<!-- 指定jacoco.exec的位置,用于生成jacoco报告 -->
						-javaagent:${settings.localRepository}/org/jacoco/org.jacoco.agent/${jacoco.version}/org.jacoco.agent-${jacoco.version}-runtime.jar=destfile=${project.basedir}/target/jacoco.exec
					</argLine>
				</configuration>
			</plugin>

更过,demo可以自行下载。

相关推荐
工业互联网专业2 分钟前
基于springboot+vue的校园二手物品交易平台
java·vue.js·spring boot·毕业设计·源码·课程设计·校园二手物品交易平台
常年游走在bug的边缘38 分钟前
基于spring boot 集成 deepseek 流式输出 的vue3使用指南
java·spring boot·后端·ai
李菠菜1 小时前
SpringBoot+Shiro同服务器多项目Cookie冲突解决方案
spring boot·后端·shiro
旅行的狮子2 小时前
二、在springboot 中使用 AIService
java·spring boot·langchain4j
一只鹿鹿鹿3 小时前
【测试文档】项目测试文档,测试管理规程,测试计划,测试文档模版,软件测试报告书(Word)
数据库·后端·spring·单元测试
jstart千语4 小时前
【SpringBoot】HttpServletRequest获取使用及失效问题(包含@Async异步执行方案)
java·前端·spring boot·后端·spring
冯诺一没有曼4 小时前
Java记账系统项目实战 | Spring Boot + MyBatis Plus + Layui 前后端整合开发
java·spring boot·mybatis
bing_1584 小时前
在 Spring Boot 项目中怎么识别和优化慢 SQL ?
spring boot·优化慢sql·识别慢sql
csdn_HPL4 小时前
SpringBoot + Vue 实现云端图片上传与回显(基于OSS等云存储)
vue.js·spring boot·后端
RainbowSea4 小时前
通用型产品发布解决方案(基于分布式微服务技术栈:SpringBoot+SpringCloud+Spring CloudAlibaba+Vue+ElementUI
java·spring boot·后端