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

JUnit 5测试无法被发现求助(Spring Boot 2.4.13环境)

问题排查:JUnit 5测试套件无法发现测试

环境信息

  • Spring Boot版本:2.4.13
  • 引入的JUnit 5依赖:
    • junit-jupiter-api 5.7.2
    • junit-bom 5.7.2
    • junit-platform-suite-api 1.8.0
    • junit-platform-suite 1.8.0

测试套件代码

package org.onap.so;

import org.junit.platform.suite.api.ExcludeTags;
import org.junit.platform.suite.api.IncludeClassNamePatterns;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@Suite
@ExcludeTags("spring-aware")
@IncludeClassNamePatterns(".*Test")
@SelectPackages("org.onap.so")
public class NonSpringSuite {
}

Maven Surefire插件配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>default-test</id>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <includes>
                    <include>**/**NonSpringSuite.java</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>

报错信息

WARNING: Third-party TestEngine implementations are forbidden to use the reserved 'junit-' prefix for their ID: 'junit-platform-suite'
[ERROR] TestEngine with ID 'junit-platform-suite' failed to discover tests

问题原因

  1. 依赖版本不兼容:JUnit Platform Suite 1.8.0与JUnit Jupiter 5.7.2版本不匹配,JUnit Platform的版本需与JUnit Jupiter保持一致(5.7.x对应JUnit Platform 1.7.x),版本错位会导致TestEngine加载异常。
  2. TestEngine ID警告本质:报错中的前缀冲突提示是版本不兼容引发的衍生问题,核心是套件依赖与JUnit核心依赖版本不匹配。
  3. Surefire配置冗余:路径写法**/**NonSpringSuite.java存在冗余,且未明确指定JUnit Platform引擎,可能影响套件识别。

解决办法

1. 统一JUnit版本,用BOM管理依赖

Spring Boot的spring-boot-starter-test已集成JUnit BOM,可自动管理版本,避免手动引入的冲突。若需单独引入Suite依赖,需保证版本与JUnit Jupiter一致(5.7.2对应JUnit Platform Suite 1.7.2)。

修改后的Maven依赖配置:

<!-- 用Spring Boot starter统一管理测试依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <!-- 可选:排除JUnit 4引擎(若不需要) -->
    <!-- <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions> -->
</dependency>

<!-- 引入JUnit Platform Suite,版本与Spring Boot管理的JUnit版本对齐 -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-suite</artifactId>
    <scope>test</scope>
</dependency>

2. 修正Surefire插件配置

简化路径并明确指定JUnit Platform引擎:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <!-- 简化套件类路径 -->
        <includes>
            <include>**/NonSpringSuite.java</include>
        </includes>
        <!-- 明确指定使用JUnit Platform引擎 -->
        <providerSelection>junit-platform</providerSelection>
    </configuration>
</plugin>

3. 验证测试套件与测试类

  • 确认@SelectPackages指向的包下存在符合.*Test命名规则的测试类。
  • 检查测试类是否使用JUnit 5注解(如@Test来自org.junit.jupiter.api.Test,而非JUnit 4的org.junit.Test)。
  • 确认测试类未被@ExcludeTags("spring-aware")规则排除。

4. 清理并重新下载依赖

执行Maven命令清理本地仓库,避免依赖损坏:

mvn clean install -U

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 10:12:47