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

Maven运行JUnit测试时Mockito依赖注入失败的问题咨询

解决Mockito在Maven测试中依赖注入失败的问题

1. 确认测试类的注解是否正确生效

你的测试类必须通过Mockito的运行器/扩展启用注解驱动的依赖注入,否则@Mock和@InjectMocks不会生效,对象会为null:

  • 若使用JUnit 5,测试类顶部必须添加:
    @ExtendWith(MockitoExtension.class)
    
  • 若使用JUnit 4,测试类顶部必须添加:
    @RunWith(MockitoJUnitRunner.class)
    

测试类示例:

@ExtendWith(MockitoExtension.class)
public class MyServiceTest {
    @Mock
    private OtherService otherService; // 模拟依赖对象

    @InjectMocks
    private MyService myService; // 待测试服务,自动注入Mock对象

    @Test
    public void testBusinessLogic() {
        // 测试逻辑代码
    }
}

注意:不要手动实例化myService或otherService,这会覆盖Mockito的注入逻辑。

2. 检查pom.xml的依赖配置(核心问题)

IntelliJ可能自动补全了测试依赖,但Maven严格遵循pom.xml配置,常见缺失或不兼容情况:

针对JUnit 5项目

必须包含mockito-junit-jupiter依赖,否则JUnit 5无法加载Mockito扩展:

<dependencies>
    <!-- JUnit 5核心依赖 -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.9.2</version>
        <scope>test</scope>
    </dependency>
    <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-core</artifactId>
        <version>4.11.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>4.11.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

针对JUnit 4项目

确保mockito-core和JUnit 4依赖版本兼容:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>3.12.4</version>
        <scope>test</scope>
    </dependency>
</dependencies>

关键:Mockito 4.x仅支持JUnit 5,3.x兼容JUnit 4/5,避免版本冲突。

3. 验证Maven Surefire插件配置

确保Surefire插件能正确识别测试类,默认匹配*Test.java,若测试类命名特殊,需手动配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.1.2</version>
            <configuration>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

使用最新版本的Surefire插件,避免旧版本的兼容性问题。

4. 统一JDK版本

IntelliJ和Maven使用的JDK版本不一致可能导致注解处理异常,在pom.xml中指定:

<properties>
    <maven.compiler.source>17</maven.compiler.source> <!-- 替换为你的项目JDK版本 -->
    <maven.compiler.target>17</maven.compiler.target>
</properties>

同时在IntelliJ的Project Structure中设置Project SDK和Module SDK与pom.xml一致。

5. 清理缓存并重新构建

执行以下命令清除Maven缓存,避免依赖冲突:

mvn clean test

若仍有问题,可删除本地Maven仓库中org.mockito和org.junit相关目录,再重新执行构建。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 19:19:50