Java maven项目打包自动测试并集成jacoco生成代码测试覆盖度报告

引入Junit

引入 junit5 单元测试依赖

xml 复制代码
		<properties>
	        <junit.version>5.10.2</junit.version>
	        <jacoco.version>0.8.12</jacoco.version>
	    </properties>
	    
	    <dependencies>
            <!-- 单元测试 -->
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
         </dependencies>

引入插件

引入 jacoco

xml 复制代码
		<properties>
	        <jacoco.version>0.8.12</jacoco.version>
	    </properties>
	    
	    <build>
			<plugins>
			    <plugin>
	                <groupId>org.jacoco</groupId>
	                <artifactId>jacoco-maven-plugin</artifactId>
	                <version>${jacoco.version}</version>
	                <executions>
	                    <execution>
	                        <id>prepare-agent</id>
	                        <goals>
	                            <goal>prepare-agent</goal>
	                        </goals>
	                    </execution>
	                    <execution>
	                        <id>report</id>
	                        <!-- 这是指定生成报告时期,注意一定要在test之后 -->
	                        <phase>prepare-package</phase>
	                        <goals>
	                            <goal>report</goal>
	                        </goals>
	                    </execution>
	                </executions>
	                <configuration>
	                	<!-- 这是报告生成位置 target/site/jacoco -->
	                    <outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
	                </configuration>
	            </plugin>
	        </plugins>
        </build>

引入 maven-surefire-plugin 插件

xml 复制代码
		<properties>
	        <jacoco.version>0.8.12</jacoco.version>
	    </properties>
	    
	    <build>
			<plugins>
			    <plugin>
	                <groupId>org.apache.maven.plugins</groupId>
	                <artifactId>maven-surefire-plugin</artifactId>
	                <version>3.2.5</version>
	            </plugin>
	        </plugins>
        </build>

配置完成后执行 mvn clean install 生成报告如下:

相关推荐
知其然亦知其所以然32 分钟前
JVM社招面试题:队列和栈是什么?有什么区别?我在面试现场讲了个故事…
java·后端·面试
harmful_sheep40 分钟前
Spring 为何需要三级缓存解决循环依赖,而不是二级缓存
java·spring·缓存
星辰大海的精灵41 分钟前
如何确保全球数据管道中的跨时区数据完整性和一致性
java·后端·架构
大大。44 分钟前
van-tabbar-item选中active数据变了,图标没变
java·服务器·前端
nc_kai1 小时前
Flutter 之 每日翻译 PreferredSizeWidget
java·前端·flutter
Codebee1 小时前
OneCode:AI时代的先锋——注解驱动技术引领开发范式变革
java
勤奋的知更鸟1 小时前
Java 编程之状态模式
java·开发语言·状态模式
架构个驾驾1 小时前
深入浅出MyBatis-Plus实战指南
java
SimonKing1 小时前
解锁万能文件内容分析工具:Apache Tika
java·后端·程序员