Mockito mockStatic 无法解析符号
Mockito mockStatic cannot resolve symbol
我正在使用 Spring 引导并在单元测试中尝试模拟 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()
方法无法解析吗?
确保你的 pom 文件中有以下依赖项
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.5.15</version>
<scope>test</scope>
</dependency>
可能该方法在您的 class 范围内,将静态方法调用 mockStatic
移至方法范围。
另外请确保您是从此 class org.powermock.api.mockito.PowerMockito.mockStatic
导入
我正在使用 Spring 引导并在单元测试中尝试模拟 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()
方法无法解析吗?
确保你的 pom 文件中有以下依赖项
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.5.15</version>
<scope>test</scope>
</dependency>
可能该方法在您的 class 范围内,将静态方法调用 mockStatic
移至方法范围。
另外请确保您是从此 class org.powermock.api.mockito.PowerMockito.mockStatic