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

运行测试生成JaCoCo覆盖率报告时遇maven-surefire-plugin执行失败

解决Maven测试阶段JaCoCo关联的Surefire失败问题

问题场景

  • 环境:Java 15.0.2、Maven 3.8.5
  • 现象:项目clean、compile操作正常,但执行测试阶段时,Surefire插件报错:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project empire-db: There are test failures.

  • 相关插件配置:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-project-info-reports-plugin</artifactId>
    <version>3.2.1</version>            
</plugin>
       
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</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>
</plugin>

解决建议

1. 获取完整测试失败详情

当前错误仅提示存在测试失败,无具体原因,需进一步排查:

  • 执行命令 mvn test -X 查看详细日志
  • 直接查看 target/surefire-reports 目录下的测试报告文件,定位具体失败的测试用例及原因(如断言失败、类加载异常、依赖缺失等)

2. 升级Surefire插件版本

Surefire 2.22.2对Java 11+版本兼容性不足,建议升级到适配Java 15的版本(如3.0.0-M7及以上),在pom.xml的build/plugins中添加或更新:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M7</version>
</plugin>

3. 确认JaCoCo版本与Java 15兼容

确保${jacoco.version}指向支持Java 15的版本(如0.8.7及以上),建议显式指定版本:

<properties>
    <jacoco.version>0.8.8</jacoco.version>
</properties>

4. 排查测试用例的JDK适配问题

检查测试代码中是否使用了Java 15不兼容的API、语法或反射操作,比如调用已废弃的方法、使用旧版字节码操作库等,针对性修改测试代码。

5. 排除JaCoCo干扰验证测试本身

临时注释JaCoCo插件配置,执行mvn test:

  • 若测试正常通过,说明是JaCoCo与环境的兼容性问题,可调整JaCoCo的report配置,明确指定类目录和源码目录:
<execution>
    <id>report</id>
    <phase>test</phase>
    <goals>
        <goal>report</goal>
    </goals>
    <configuration>
        <classesDirectory>${project.build.outputDirectory}</classesDirectory>
        <sourceDirectories>${project.build.sourceDirectory}</sourceDirectories>
        <encoding>UTF-8</encoding>
    </configuration>
</execution>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 14:01:35