SpringBoot+Maven+Jacoco完成本地项目测试覆盖率分析

Maven单模块

只需要在项目的pom.xml文件中添加如下的配置即可:

<plugin>
	<groupId>org.jacoco</groupId>
	<artifactId>jacoco-maven-plugin</artifactId>
	<version>0.8.6</version>
	<executions>
		<execution>
			<goals>
				<goal>prepare-agent</goal>
			</goals>
		</execution>
		<execution>
			<id>report</id>
			<phase>prepare-package</phase>
			<goals>
				<goal>report</goal>
			</goals>
		</execution>
	</executions>
</plugin>

Maven多模块

Jacoco可以很好的支持对Maven多模块进行聚合分析测试覆盖率,可以从项目整体输出覆盖率报告非常方便。
假设现有的Project结构如下(大部分的多Maven模块是这种结构):

├── parent-project
├── pom.xml
├── business-module1
│   ├── pom.xml
│   └── src
│       ├── main
│       └── test
├── business-module2
    ├── pom.xml
    └── src
        ├── main
        └── test

需要添加一个test-module变成如下的结构

├── parent-project
├── pom.xml
├── business-module1
│   ├── pom.xml
│   └── src
│       ├── main
│       └── test
├── business-module2
│   ├── pom.xml
│   └── src
│       ├── main
│       └── test
└── test-module
    ├── pom.xml
    └── src
        ├── main
        └── test

接下来是配置的重点:
根目录的pom.xml配置:
1、 将test-module作为子模块
2、jacoco插件并且启动代理

// modules中增加test-module
  <modules>
    <module>business-module1</module>
    <module>business-module2</module>
    <module>test-module</module>
  </modules>
// plugin中增加
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.6</version>
  <executions>
    <execution>
      <id>prepare-agent</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
    <execution>
      <id>report</id>
      <phase>verify</phase>
      <goals>
        <goal>report</goal>
      </goals>
    </execution>
  </executions>
</plugin>

test-module的pom.xml
1、添加现有Project的子module的依赖
2、引入jacoco插件,声明一个聚合分析任务

<dependency>
    <groupId>按照实际配置</groupId>
    <artifactId>business-module1</artifactId>
    <version>按照实际配置</version>
</dependency>
<dependency>
    <groupId>按照实际配置</groupId>
    <artifactId>business-module1</artifactId>
    <version>按照实际配置</version>
</dependency>


<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.6</version>
    <executions>
        <execution>
            <id>report-aggregate</id>
            <phase>verify</phase>
            <goals>
                <goal>report-aggregate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

business-module1的pom.xml
引入jacoco插件

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
</plugin>

business-module2的pom.xml
引入jacoco插件

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
</plugin>

运行

构建成功后可以在test-module模块下的target/site/jacoco-aggregate/index.html 查看覆盖率报告:

常见问题

1、business-module1模块或者business-module2模块下的target目录没有生成jacoco.exec文件
查看Maven的构建日志,看是否打印?

[INFO] --- jacoco-maven-plugin:0.8.6:prepare-agent (prepare-agent) @ business-module1 ---
[INFO] argLine set to -javaagent:C:\\Users\\bxu14\\.m2\\repository\\org\\jacoco\\org.jacoco.agent\\0.8.6\\org.jacoco.agent-0.8.6-runtime.jar=destfile=C:\\Users\\bxu14\\Desktop\\source\\parent-project\\business-module1\\target\\jacoco.exec

如果正常打印出来,则大概率就是对应的module下面并没有写Test案例导致。补充案例之后可以解决。
如果没有打印出来,那么问题就出在配置上面,按照上面的教程检查下每个module是否都改全了。
有如下几个思路:
a、每个业务module下面是否引入jacoco插件。
b、

2、Skipping JaCoCo execution due to missing execution data file.
基本上就是因为没有jacoco.exec文件导致的,按照方法1的解决方案即可。

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐