Mockito mockStatic无法解析符号

3qpi33ja  于 2022-11-08  发布在  其他
关注(0)|答案(2)|浏览(274)

我正在使用Spring Boot,并且在单元测试中,我正在尝试模拟Files.delete(myFile.toPath())方法。
为此,我尝试使用Mockito.mockStatic()方法。但是当我尝试使用它时,我的IDE(IntelliJ IDEA)提示我该方法不存在。我阅读了这篇文章:但这对我没什么帮助。
在我的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()方法不能被解析吗?

wswtfjt7

wswtfjt71#

请确保pom文件中具有以下依赖项

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

s6fujrry2#

方法可能在您的类别范围内,请将静态方法呼叫mockStatic移至方法范围。
还要确保从org.powermock.api.mockito.PowerMockito.mockStatic类导入

相关问题