Jacoco Maven集成

Maven是Java项目常用的一款项目管理工具,我们可以通过Maven进行项目的编译、测试、打包等操作。Jacoco与Maven的整合,通过Maven命令执行,获取Jacoco生成的覆盖率测试报告,也以便于与Jenkins集成,接下来对我们的Maven工程进行Jacoco的整合。

Maven工程中添加Jacoco插件

Jacoco Maven插件官网说明

使用的必须条件:

  • Maven的版本3.0以上
  • Java的版本1.5以

在我们maven项目的pom.xml中引入如下的模块与配置

1
2
3
4
5
6
7
8
9
10
<build>
<plugins>
<!--jacoco的插件-->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7-SNAPSHOT</version>
</plugin>
</plugins>
</build>

使用以下命令,可以查看Jacoco Maven的帮助文档

1
mvn help:describe -Dplugin=org.jacoco:jacoco-maven-plugin -Ddetail

Maven执行配置

以上是引入了Jacoco的插件,执行单元测试mvn clean install执行结束后,发现未生成Jacoco的测试报告,这时就要配置明确指定执行时的报告集:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!--这里的execution ,每一个执行的goal,对应的id必须是唯一的-->
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!--这个check:对代码进行检测,控制项目构建成功还是失败-->
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
<!--report:对代码进行检测,然后生成index.html在 target/site/index.html中可以查看检测的详细结果-->
<execution>
<id>report</id>
<phase>prepare-package</phase>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<!--report:对代码进行检测,然后生成index.html在 target/site/index.html中可以查看检测的详细结果, 也可指定jacoco报告输出目录,报告在target/jacoco-ut/index.html查看-->
<configuration>
<dataFile>target/jacoco.exec</dataFile>
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>

根据官网提供整理了以下插件提供的goal:

操作 调用 说明
check org.jacoco.maven.CheckMojo Checks that the code coverage metrics are being met
dump org.jacoco.maven.DumpMojo Request a dump over TCP/IP from a JaCoCo agent running in tcpserver mode.
help org.jacoco.maven.HelpMojo Display help information on jacoco-maven-plugin
instrument org.jacoco.maven.InstrumentMojo Performs offline instrumentation. Note that after execution of test you must restore original classes with help of "restore-instrumented-classes" goal
merge org.jacoco.maven.MergeMojo Mojo for merging a set of execution data files (*.exec) into a single file
prepare-agent org.jacoco.maven.AgentMojo Prepares a property pointing to the JaCoCo runtime agent that can be passed as a VM argument to the application under test.
prepare-agent-integration org.jacoco.maven.AgentITMojo Same as code prepare-agent code, but provides default values suitable for integration-tests
report org.jacoco.maven.ReportMojo Creates a code coverage report for tests of a single project in multiple formats (HTML, XML, and CSV).
report-aggregate org.jacoco.maven.ReportAggregateMojo Creates a structured code coverage report (HTML, XML, and CSV) from multiple projects within reactor. The report is created from all modules this project depends on. From those projects class and source files as well as JaCoCo execution data files will be collected. In addition execution data is collected from the project itself. This also allows to create coverage reports when tests are in separate projects than the code under test, for example in case of integration tests.
report-integration org.jacoco.maven.ReportITMojo Same as code report code, but provides default values suitable for integration-tests
restore-instrumented-classes org.jacoco.maven.RestoreMojo Restores original classes as they were before offline instrumentation.

根据提供的执行目标,根据需求进行配置。

配置完成的执行目标,再次执行mvn clean install,这时查看工程执行后的target目录下就已生成jacoco.exec文件和jacoco-ut目录:

image-20201020165351082

代码覆盖率报告如下:

image-20201020165437334

report官网说明

Maven包含/排除覆盖的文件

对于代码覆盖率,主要是执行逻辑代码的覆盖,对于Config、Entity、VO、文件等,这些配置、实体相关的就不会覆盖到,但是在统计代码覆盖情况时,又会将这些代码覆盖,因此在配置的时候,可将不需要的包配置排除或配置需要包含的包

  • exclude 排除
  • include 包含
1
2
3
4
5
6
7
8
9
10
11
12
<configuration>
<!--排除不需要的覆盖的文件-->
<excludes>
<exclude>com/spring/boot/demo/common/config/*</exclude>
<exclude>com/spring/boot/demo/mapper/*</exclude>
<exclude>com/spring/boot/demo/model/**/*</exclude>
</excludes>
<!--包含需要覆盖的文件-->
<includes>
<include>com/spring/boot/demo/controller/*</include>
</includes>
</configuration>

配置完成,执行测试,产生的报告就会根据对应配置的文件,进行覆盖率的统计

排除字段的写法相对于目录(包)、类,使用标准的通配符语法编译后类的路径(而不是包名称)

* Match zero or more characters
** Match zero or more directories
? Match a single character

image-20201021120209177

根据配置统计不包含的文件后,得出的覆盖率报告,则不会统计及显示已排除的文件

image-20201022173537685

查看报告,覆盖率也有明显的提升。

Maven规则配置

根据代码里的注释可知,规则配置可对于对象类型,覆盖率维度,定义最大值、最小值的阈值

  • 对象类型,标签<element>
    • BUNDLE 约束
    • PACKAGE 包
    • CLASS 类
    • SOURCEFILE 源文件
    • METHOD 方法
  • 覆盖率维度,标签<counter>
    • INSTRUCTION 指令
    • LINE 行
    • BRANCH 分支
    • COMPLEXITY 复杂度
    • METHOD 方法
    • CLASS 类
  • 统计覆盖率进行阈值的限制,覆盖率类型标签<value>
    • TOTALCOUNT
    • COVEREDCOUNT
    • MISSEDCOUNT
    • COVEREDRATIO
    • MISSEDRATIO
  • 数值的设置,最大值、最小值,标签<maximum> or <minimum>
    • 数值范围:必须在0.0到1.0的范围内

覆盖率执行的情况到什么程度,才是完成测试通过呢?这时可根据配置规则进行规则的限制。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<configuration>
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<!-- 指定方法覆盖到80% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>METHOD</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<!-- 指定指令覆盖到80% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<!-- 指定行覆盖到80% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<!-- 指定类覆盖到100%,不能遗失任何类 -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>

配置完成后,执行测试,如果达不到设置的覆盖率,则执行失败

image-20201020171013508

报错信息:Coverage checks have not been met. See log for details.