英文:
Mockito mockStatic cannot resolve symbol
问题
我正在使用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;
同样,这只是与测试相关的导入。
你有任何关于为什么Mockito.mockStatic()
方法无法解析的想法吗?
英文:
I'm using Spring Boot and in a unit test, I'm trying to mock the Files.delete(myFile.toPath())
method.
To do so I'm trying to use the Mockito.mockStatic()
method. But when I try to use it my IDE (IntelliJ IDEA) indicate me that this method does not exist.
I read this article :
https://asolntsev.github.io/en/2020/07/11/mockito-static-methods/
but it didn't help me.
In my POM.xml file there is:
<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>
Note that I only put test related dependency, this is not my whole POM.xml file
In my test file, I put the following imports:
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;
Again, this is only the test related imports.
A screenshot of what my IDE is displaying:
Do you have any idea why the Mockito.mockStatic()
method can't be resolved ?
答案1
得分: 17
请确保您的pom文件中包含以下依赖项:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.5.15</version>
<scope>test</scope>
</dependency>
英文:
Make sure you have the following dependency in your pom file
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.5.15</version>
<scope>test</scope>
</dependency>
答案2
得分: 0
可能该方法位于您的类范围内,请将静态方法调用“mockStatic”移动到方法范围内。
还要确保您从这个类org.powermock.api.mockito.PowerMockito.mockStatic
导入。
英文:
Probably the method is inside your class scope, move the static method call mockStatic
to method scope.
Also be sure you're importing from this class org.powermock.api.mockito.PowerMockito.mockStatic
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论