springboot maven 多模块单元测试 Jacoco
时间: 2023-11-25 16:07:55 浏览: 184
可以使用 Maven 进行 springboot 多模块单元测试和 Jacoco 代码覆盖率统计。
对于多模块项目,需要在父 POM 文件中添加 Jacoco 插件配置,以及指定子模块的测试目录:
```xml
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report-aggregate</id>
<phase>test</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>unit-test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<skipTests>false</skipTests>
<skip>false</skip>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
<argLine>${jacoco.agent.argLine}</argLine>
<excludedGroups>org.junit.experimental.categories.ExcludeCategory</excludedGroups>
<testFailureIgnore>true</testFailureIgnore>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
<profiles>
<profile>
<id>unit-test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<skipTests>false</skipTests>
<skip>false</skip>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
<argLine>${jacoco.agent.argLine}</argLine>
<excludedGroups>org.junit.experimental.categories.ExcludeCategory</excludedGroups>
<testFailureIgnore>true</testFailureIgnore>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
```
这个配置会在编译和测试时启动 Jacoco 代理,同时在测试后生成 Jacoco 报告。
同时,可以使用 Maven Profiles 来区分测试和生产环境的配置,例如上面的配置中就定义了一个名为 `unit-test` 的 Profile。在执行单元测试时,可以使用命令 `mvn clean test -Punit-test` 来指定使用 `unit-test` Profile。
对于单个模块的项目,可以在模块的 POM 文件中添加 Jacoco 插件配置:
```xml
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
```
这个配置会在编译和测试时启动 Jacoco 代理,同时在测试后生成 Jacoco 报告。
阅读全文