【问题标题】:Mockito mockStatic cannot resolve symbolMockito mockStatic 无法解析符号
【发布时间】:2021-02-04 06:29:34
【问题描述】:

我正在使用 Spring Boot,在单元测试中,我试图模拟 Files.delete(myFile.toPath()) 方法。
为此,我正在尝试使用Mockito.mockStatic() 方法。但是当我尝试使用它时,我的 IDE(IntelliJ IDEA)告诉我这种方法不存在。 我读了这篇文章: https://asolntsev.github.io/en/2020/07/11/mockito-static-methods/ 但这对我没有帮助。

在我的 POM.xml 文件中有:

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>3.5.15</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>3.5.15</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <version>2.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

注意我只放了测试相关的依赖,这不是我的整个 POM.xml 文件

在我的测试文件中,我输入了以下内容:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

同样,这只是与测试相关的导入。

我的 IDE 显示的屏幕截图:

您知道为什么Mockito.mockStatic() 方法无法解析吗?

【问题讨论】:

    标签: java spring spring-boot unit-testing mockito


    【解决方案1】:

    确保您的 pom 文件中有以下依赖项

            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>3.5.15</version>
                <scope>test</scope>
            </dependency>
    

    【讨论】:

    • 谢谢!有效 !这篇文章说用mockito-inline 替换mockito-core,我应该试着把两者都放。
    • 是的,我得到了同样的东西。谢谢
    • 非常感谢你救了你一命!
    猜你喜欢
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 2016-12-31
    • 2018-12-12
    • 1970-01-01
    相关资源
    最近更新 更多