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

Maven Failsafe Plugin仅执行单个测试问题求助

问题排查:Maven Failsafe Plugin仅执行单个测试的原因及解决办法

先梳理几个关键问题点,这些大概率是导致你遇到异常现象的根源:


1. 错误地将插件作为项目依赖引入

你在<dependencies>块中添加了maven-failsafe-plugin和maven-surefire-plugin的依赖,这是完全错误的用法!Maven插件不需要作为项目依赖引入,只需要在<build><plugins>节点下配置即可。这些多余的依赖会干扰插件的正常加载逻辑,甚至引发依赖冲突,导致测试识别异常。

解决办法:删除<dependencies>中的这两个插件依赖:

<!-- 移除这两个错误的依赖配置 -->
<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.22.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
</dependency>

2. JUnit 5依赖版本不一致且未利用Spring Boot的依赖管理

你的JUnit Jupiter API(5.5.2)和Engine(5.3.1)版本不匹配,这会导致测试引擎无法正常识别和执行测试类。另外,Spring Boot Parent已经内置了JUnit 5的版本管理,手动指定版本反而容易引发依赖冲突。

解决办法:改用Spring Boot官方的spring-boot-starter-test依赖,它会自动管理所有测试相关的依赖(包括JUnit 5、AssertJ、Mockito等),替换掉你当前的两个JUnit依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <!-- 若需排除JUnit 4兼容依赖,可添加以下配置 -->
    <!--
    <exclusions>
        <exclusion>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </exclusion>
    </exclusions>
    -->
</dependency>

3. 确认测试类的命名与注解配置

虽然你配置了Failsafe的<includes>为**/*,但要确保:

  • 所有测试类都是.java后缀(你的目录结构里写的TodoRestDocumentation、TestIntegrationTest可能是简写,实际要确认文件名是TodoRestDocumentation.java这类完整后缀)
  • 测试方法上是否添加了JUnit 5的@Test注解(全类名org.junit.jupiter.api.Test),没有注解的方法不会被执行

4. 确认执行命令的正确性

Maven Failsafe Plugin的集成测试是在integration-test和verify阶段执行的,如果你运行的是mvn test,只会触发Surefire的单元测试,不会执行Failsafe的集成测试。你需要执行:

mvn verify
# 或者单独执行集成测试阶段
mvn integration-test

5. 检查测试类是否被正确编译

虽然你提到测试已编译到target目录,但可以手动确认target/test-classes下是否存在TodoRestDocumentation.class、TestIntegrationTest.class这些文件。如果不存在,说明编译过程有问题,可执行mvn clean test-compile强制重新编译测试类。


按照以上步骤逐一排查,应该能解决Failsafe仅执行单个测试的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:00:45