Jacoco Maven集成
Jacoco Maven集成
Maven是Java项目常用的一款项目管理工具,我们可以通过Maven进行项目的编译、测试、打包等操作。Jacoco与Maven的整合,通过Maven命令执行,获取Jacoco生成的覆盖率测试报告,也以便于与Jenkins集成,接下来对我们的Maven工程进行Jacoco的整合。
Maven工程中添加Jacoco插件
使用的必须条件:
- Maven的版本3.0以上
- Java的版本1.5以
在我们maven项目的pom.xml中引入如下的模块与配置
1 | <build> |
使用以下命令,可以查看Jacoco Maven的帮助文档
1 | mvn help:describe -Dplugin=org.jacoco:jacoco-maven-plugin -Ddetail |
Maven执行配置
以上是引入了Jacoco的插件,执行单元测试mvn clean install
执行结束后,发现未生成Jacoco的测试报告,这时就要配置明确指定执行时的报告集:
1 | <!--这里的execution ,每一个执行的goal,对应的id必须是唯一的--> |
根据官网提供整理了以下插件提供的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目录:
代码覆盖率报告如下:
Maven包含/排除覆盖的文件
对于代码覆盖率,主要是执行逻辑代码的覆盖,对于Config、Entity、VO、文件等,这些配置、实体相关的就不会覆盖到,但是在统计代码覆盖情况时,又会将这些代码覆盖,因此在配置的时候,可将不需要的包配置排除或配置需要包含的包
- exclude 排除
- include 包含
1 | <configuration> |
配置完成,执行测试,产生的报告就会根据对应配置的文件,进行覆盖率的统计
排除字段的写法相对于目录(包)、类,使用标准的通配符语法编译后类的路径(而不是包名称)
*
Match zero or more characters**
Match zero or more directories?
Match a single character
根据配置统计不包含的文件后,得出的覆盖率报告,则不会统计及显示已排除的文件
查看报告,覆盖率也有明显的提升。
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 | <configuration> |
配置完成后,执行测试,如果达不到设置的覆盖率,则执行失败
报错信息:Coverage checks have not been met. See log for details.