You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为OpenLiberty集成测试生成Jacoco覆盖率报告?

为OpenLiberty集成测试生成Jacoco覆盖率报告

完全可以为这类通过Maven Failsafe插件运行的OpenLiberty集成测试生成Jacoco覆盖率报告,核心是让Jacoco在OpenLiberty服务器启动时注入覆盖率收集代理,再结合插件配置完成数据收集与报告生成,具体步骤如下:

1. 为OpenLiberty服务器配置Jacoco Agent

在项目pom.xml中,给liberty-maven-plugin添加JVM参数,让服务器启动时加载Jacoco代理,指定集成测试覆盖率数据的输出文件:

<plugin>
    <groupId>io.openliberty.tools</groupId>
    <artifactId>liberty-maven-plugin</artifactId>
    <version>3.10.1</version>
    <configuration>
        <serverName>testServer</serverName>
        <jvmOptions>
            <param>-javaagent:${settings.localRepository}/org/jacoco/jacoco/${jacoco.version}/jacocoagent.jar=destfile=${project.build.directory}/jacoco-it.exec,append=true</param>
        </jvmOptions>
    </configuration>
</plugin>
  • destfile:指定集成测试覆盖率数据的输出路径
  • append=true:避免覆盖单元测试生成的覆盖率文件

2. 关联Failsafe插件与Jacoco

确保Failsafe插件运行集成测试时,Jacoco能正确触发数据收集,需要在Failsafe配置中引入Jacoco插件依赖:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.0.0-M9</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
        </dependency>
    </dependencies>
</plugin>

3. 配置Jacoco生成集成测试报告

在Jacoco插件的执行配置中,添加集成测试相关的目标,支持生成独立的集成测试报告或合并单元+集成测试的总报告:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <executions>
        <!-- 保留已有的单元测试覆盖率配置 -->
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>default-report</id>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>

        <!-- 集成测试覆盖率收集与报告 -->
        <execution>
            <id>prepare-agent-integration</id>
            <goals>
                <goal>prepare-agent-integration</goal>
            </goals>
        </execution>
        <execution>
            <id>report-integration</id>
            <goals>
                <goal>report-integration</goal>
            </goals>
            <configuration>
                <dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
            </configuration>
        </execution>

        <!-- 可选:生成单元+集成测试的合并覆盖率报告 -->
        <execution>
            <id>report-aggregate</id>
            <phase>verify</phase>
            <goals>
                <goal>report-aggregate</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

4. 执行构建生成报告

运行Maven命令:

mvn clean verify

执行流程:

  1. 运行单元测试并收集覆盖率数据
  2. 启动带有Jacoco代理的OpenLiberty服务器
  3. 运行集成测试,收集覆盖率数据到jacoco-it.exec
  4. 生成对应报告

报告输出位置:

  • 单元测试报告:target/site/jacoco
  • 集成测试报告:target/site/jacoco-it
  • 合并总报告(可选):target/site/jacoco-aggregate

注意事项

  • 确保pom.xml的<properties>中定义了Jacoco版本,比如:<jacoco.version>0.8.10</jacoco.version>
  • 多模块项目中,report-aggregate目标需要在父模块配置,并指定要包含的子模块
  • 确保OpenLiberty服务器在集成测试完成后正常停止,避免覆盖率数据写入不完整

内容的提问来源于stack exchange,提问作者Javadee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 15:00:01