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

Maven未执行测试求助:JUnit5+Mockito环境下测试类不运行

排查Maven不执行JUnit 5测试的问题

我来帮你捋捋这个坑——Eclipse里能正常跑测试,但Maven执行clean install或test时测试数为0,这种情况基本都和Maven的测试配置、依赖或者扫描规则有关,咱们一步步排查:

1. 先确认JUnit 5和Mockito的依赖是否正确

Maven对测试依赖的范围和版本很敏感,先检查你的pom.xml里有没有正确引入JUnit Jupiter的核心依赖:

<dependencies>
    <!-- JUnit 5 API -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.9.2</version> <!-- 用你实际使用的版本 -->
        <scope>test</scope>
    </dependency>
    <!-- JUnit 5 测试引擎 -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.9.2</version>
        <scope>test</scope>
    </dependency>
    <!-- Mockito与JUnit 5集成 -->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>4.11.0</version> <!-- 匹配你的Mockito版本 -->
        <scope>test</scope>
    </dependency>
</dependencies>

重点:不要漏了junit-jupiter-engine,这是Maven执行JUnit 5测试的核心引擎,没有它的话Surefire插件识别不到测试。

2. 检查Maven Surefire插件的配置

Maven默认用Surefire插件执行测试,但老版本的Surefire不支持JUnit 5,必须明确配置支持JUnit Jupiter:

<build>
    <plugins>
        <!-- 配置Surefire插件支持JUnit 5 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M7</version> <!-- 用3.0+的版本 -->
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.9.2</version> <!-- 和上面的引擎版本一致 -->
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

如果你的Surefire版本低于2.22.0,基本肯定识别不了JUnit 5的测试,必须升级。

3. 确认测试类的规则符合Maven扫描逻辑

Maven Surefire默认只会扫描符合命名规则的测试类:

  • 类名以Test开头/结尾(你的TestUT1、TestIT是符合的)
  • 类中标注了JUnit 5的@Test注解(注意是org.junit.jupiter.api.Test,不是JUnit 4的org.junit.Test!)

如果你的测试类用了JUnit 4的注解,Maven会直接忽略,而Eclipse可能兼容两种注解所以能跑,这是常见的坑。

4. 区分单元测试和集成测试的执行逻辑

你的TestIT是集成测试类,Maven默认:

  • mvn test只会执行单元测试(由Surefire插件处理)
  • 集成测试需要用maven-failsafe-plugin,并且执行mvn verify才会触发

如果你的TestIT是集成测试,那mvn test时不会执行它,而Eclipse右键运行会不管类型直接执行,这就会造成“Eclipse能跑全量,Maven只跑了部分甚至0个”的错觉。可以给Failsafe插件加配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.0.0-M7</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.2</version>
        </dependency>
    </dependencies>
</plugin>

之后执行mvn verify就能触发集成测试了。

5. 用调试日志找线索

如果上面的配置都没问题,执行mvn test -X开启调试日志,重点看Surefire插件的扫描日志:

  • 有没有输出“Found X test classes”之类的信息?
  • 有没有报错说找不到测试引擎,或者测试类不符合规则?

日志里会明确告诉你Maven为什么没执行测试,是最直接的排查手段。

内容的提问来源于stack exchange,提问作者M.R.M

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:49:50