首页 > 编程语言 >使用jacoco-maven-plugin生成Java项目代码覆盖率报告

使用jacoco-maven-plugin生成Java项目代码覆盖率报告

时间:2023-01-05 18:44:06浏览次数:61  
标签:java Java plugin maven test org jacoco

1. 参考资料

https://www.cnblogs.com/fnlingnzb-learner/p/10637802.html
https://maven.apache.org/surefire/maven-failsafe-plugin/examples/skipping-tests.html#

2. 利用Spring initializr生成代码并添加Unit test及Integraiton test类

https://github.com/hivsuper/study/tree/master/study-java11

3. 添加maven-surefire-plugin,maven-failsafe-plugin及jacoco-maven-plugin配置

点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.7</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>org.lxp</groupId>
	<artifactId>study-java11</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>study-java11</name>
	<description>Demo project for Java 11</description>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
    <properties>
        <java.version>11</java.version>
        <maven.compiler.release>${java.version}</maven.compiler.release>
        <jacoco-maven-plugin.version>0.8.8</jacoco-maven-plugin.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Unit test with Maven Surefire plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <parallel>method</parallel>
                    <threadCountMethods>10</threadCountMethods>
                    <excludes>
                        <exclude>**/*IT.java</exclude>
                        <exclude>**/IT*.java</exclude>
                    </excludes>
                    <systemPropertyVariables>
                        <listener>org.sonar.java.jacoco.JUnitListener</listener>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <!-- Integration test with Maven failsafe plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${maven-failsafe-plugin.version}</version>
                <executions>
                    <execution>
                        <id>integration-tests</id>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <excludes>
                                <exclude>**/Test*.java</exclude>
                                <exclude>**/*Test.java</exclude>
                            </excludes>
                            <additionalClasspathElements>
                                <additionalClasspathElement>
                                    ${project.build.directory}/${project.artifactId}-${project.version}.jar.original
                                </additionalClasspathElement>
                            </additionalClasspathElements>
                            <systemPropertyVariables>
                                <listener>org.sonar.java.jacoco.JUnitListener</listener>
                            </systemPropertyVariables>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>coverage</id>
            <activation>
                <property>
                    <name>coverage</name>
                    <value>true</value>
                </property>
            </activation>
            <properties>
                <test.listeners>org.sonar.java.jacoco.JUnitListener</test.listeners>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>${jacoco-maven-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>pre-unit-test</id>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>report</id>
                                <goals>
                                    <goal>report</goal>
                                </goals>
                                <configuration>
                                    <dataFile>target/jacoco.exec</dataFile>
                                    <outputDirectory>target/jacoco-ut</outputDirectory>
                                </configuration>
                            </execution>
                            <execution>
                                <id>pre-integration-test</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>prepare-agent-integration</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

3.1 只使用verify + Unit test生成代码覆盖率

Maven命令及输出如下
mvn clean verify -P coverage -DskipITs=true -Dmaven.test.skip=false -Dskip.repackage=true -f pom.xml

如下图所示,Integration test都被跳过

打开target/jacoco-ut中的报告

3.2 使用verify + Unit test + Integration test生成代码覆盖率

修改jacoco-maven-plugin配置
<dataFile>target/jacoco-it.exec</dataFile>
Maven命令及输出如下
mvn clean verify -P coverage -DskipITs=false -Dmaven.test.skip=false -Dskip.repackage=true -f pom.xml

3.3 使用test + Unit test生成代码覆盖率

修改jacoco-maven-plugin配置

                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>${jacoco-maven-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>pre-unit-test</id>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>report</id>
                                <goals>
                                    <goal>report</goal>
                                </goals>
                                <configuration>
                                    <dataFile>target/jacoco.exec</dataFile>
                                    <outputDirectory>target/jacoco-ut</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

Maven命令及输出如下
clean test -P coverage org.jacoco:jacoco-maven-plugin:report -Dmaven.test.skip=false -Dskip.repackage=true -f pom.xml

在target\site\jacoco中打开报告

标签:java,Java,plugin,maven,test,org,jacoco
From: https://www.cnblogs.com/hiver/p/17028615.html

相关文章

  • Triple 协议支持 Java 异常回传的设计与实现
    作者:陈景明背景在一些业务场景,往往需要自定义异常来满足特定的业务,主流用法是在catch里抛出异常,例如:publicvoiddeal(){try{//doSomething...}catch(IGreet......
  • Triple 协议支持 Java 异常回传的设计与实现
    作者:陈景明背景在一些业务场景,往往需要自定义异常来满足特定的业务,主流用法是在catch里抛出异常,例如:publicvoiddeal(){try{//doSomething...}catc......
  • JavaScript: symbol 和 string key 取值时的怪异现象
    ''做key可以被.或者[]运算符取出[""]做key同样可以被.或者[]运算符取出symbol做key只能被.取出[symbol]做key只能被[]取出......
  • javaFX中Label如何设置居中
    Labellabel=newLabel("居中");HBoxhBox=newHBox();hBox.getChildren().addAll(label);hBox.setPrefHeight(35);hBox.setPrefWidth(410);hBox.setLayoutX(125);//hBo......
  • javaFX中Label如何设置居中
    Labellabel=newLabel("居中");HBoxhBox=newHBox();hBox.getChildren().addAll(label);hBox.setPrefHeight(35);hBox.setPrefWidth(410);hBox.setLayoutX(125);//hBo......
  • javaFX中Label如何设置居中
    Labellabel=newLabel("居中");HBoxhBox=newHBox();hBox.getChildren().addAll(label);hBox.setPrefHeight(35);hBox.setPrefWidth(410);hBox.setLayoutX(125);//hBo......
  • WebStorm——最智能的Javascript IDE
    ​ WebStorm是什么?WebStorm是JetBrains的一个专门为Web开发人员设计的IDE,JetBrains大家应该不陌生,Resharper、IntelliJIDEA等都是出自这个公司。JetBrains给WebStorm下的......
  • java中for 的几种常见用法
    J2SE1.5提供了另一种形式的for循环。借助这种形式的for循环,可以用更简单地方式来遍历数组和Collection等类型的对象。本文介绍使用这种循环的具体方式,说明如何自行定义能被......
  • JavaSE复习
    面向对象面向对象编程(Object-OrientedProgramming/OOP)面向对象编程的本质:以类的方式组织代码,以对象的方式封装数据三大特性:封装、继承、多态从认识论角度考虑......
  • JavaScript实现浏览器端大文件分片上传
    ​ 前言:因自己负责的项目(jetty内嵌启动的SpringMvc)中需要实现文件上传,而自己对java文件上传这一块未接触过,且对Http协议较模糊,故这次采用渐进的方式来学习文件上传的......