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

Maven无法识别JUnit5测试但IntelliJ可正常运行的问题

Maven无法识别JUnit5测试的解决办法

问题本质

日志显示Maven自动选用了org.apache.maven.surefire.junit4.JUnit4Provider,导致JUnit5测试无法被识别,核心问题出在三个地方:

  • JUnit依赖冗余、版本不统一,BOM版本和其他JUnit依赖版本冲突
  • Maven Surefire插件未明确配置JUnit5支持
  • 部分测试依赖的scope未正确设置为test

修复步骤

1. 精简并统一JUnit依赖

用junit-bom统一管理版本,移除重复依赖(junit-jupiter已经包含junit-jupiter-api和junit-jupiter-engine,无需单独引入):

<!-- 先在dependencyManagement中导入BOM统一版本 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.9.2</version> <!-- 和其他JUnit依赖版本保持一致 -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- Mockito相关依赖,添加test scope -->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-inline</artifactId>
        <version>5.2.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>4.4.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <scope>test</scope>
    </dependency>
    
    <!-- JUnit5核心依赖 -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <scope>test</scope>
    </dependency>
    
    <!-- 移除junit-jupiter-api、junit-platform-engine等冗余依赖 -->
</dependencies>

2. 配置Surefire插件支持JUnit5

在build/plugins中显式配置Surefire插件,指定使用JUnit5测试提供者:

<build>
    <plugins>
        <!-- 保留原有插件,新增Surefire配置 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.2.2</version> <!-- 使用最新稳定版 -->
            <configuration>
                <includes>
                    <include>**/*Test.java</include> <!-- 确保测试类被扫描到 -->
                </includes>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit-platform</artifactId>
                    <version>3.2.2</version> <!-- 版本必须和插件一致 -->
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

3. 执行测试验证

运行以下命令清理项目并重新执行测试:

mvn clean test

额外注意事项

  • 确认测试类使用的是JUnit5的注解(@Test来自org.junit.jupiter.api.Test,不是JUnit4的org.junit.Test)
  • 如果测试类命名不符合*Test规则,需要在Surefire的includes中添加对应的匹配规则

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 15:01:02