Mockito的mockStatic无法解析符号。

huangapple go评论123阅读模式
英文:

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文件中有:

  1. <dependency>
  2. <groupId>org.mockito</groupId>
  3. <artifactId>mockito-inline</artifactId>
  4. <version>3.5.15</version>
  5. <scope>test</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.mockito</groupId>
  9. <artifactId>mockito-junit-jupiter</artifactId>
  10. <version>3.5.15</version>
  11. <scope>test</scope>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-test</artifactId>
  16. <scope>test</scope>
  17. <version>2.2.6.RELEASE</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.security</groupId>
  21. <artifactId>spring-security-test</artifactId>
  22. <scope>test</scope>
  23. </dependency>

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

在我的测试文件中,我添加了以下导入:

  1. import org.junit.Test;
  2. import org.junit.runner.RunWith;
  3. import org.mockito.Mockito;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.boot.test.mock.mockito.MockBean;
  6. import org.springframework.test.context.junit4.SpringRunner;

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

我的IDE显示的截图:
Mockito的mockStatic无法解析符号。

你有任何关于为什么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:

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.mockito&lt;/groupId&gt;
  3. &lt;artifactId&gt;mockito-inline&lt;/artifactId&gt;
  4. &lt;version&gt;3.5.15&lt;/version&gt;
  5. &lt;scope&gt;test&lt;/scope&gt;
  6. &lt;/dependency&gt;
  7. &lt;dependency&gt;
  8. &lt;groupId&gt;org.mockito&lt;/groupId&gt;
  9. &lt;artifactId&gt;mockito-junit-jupiter&lt;/artifactId&gt;
  10. &lt;version&gt;3.5.15&lt;/version&gt;
  11. &lt;scope&gt;test&lt;/scope&gt;
  12. &lt;/dependency&gt;
  13. &lt;dependency&gt;
  14. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  15. &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
  16. &lt;scope&gt;test&lt;/scope&gt;
  17. &lt;version&gt;2.2.6.RELEASE&lt;/version&gt;
  18. &lt;/dependency&gt;
  19. &lt;dependency&gt;
  20. &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
  21. &lt;artifactId&gt;spring-security-test&lt;/artifactId&gt;
  22. &lt;scope&gt;test&lt;/scope&gt;
  23. &lt;/dependency&gt;

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:

  1. import org.junit.Test;
  2. import org.junit.runner.RunWith;
  3. import org.mockito.Mockito;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.boot.test.mock.mockito.MockBean;
  6. import org.springframework.test.context.junit4.SpringRunner;

Again, this is only the test related imports.

A screenshot of what my IDE is displaying:
Mockito的mockStatic无法解析符号。

Do you have any idea why the Mockito.mockStatic() method can't be resolved ?

答案1

得分: 17

请确保您的pom文件中包含以下依赖项:

  1. <dependency>
  2. <groupId>org.mockito</groupId>
  3. <artifactId>mockito-core</artifactId>
  4. <version>3.5.15</version>
  5. <scope>test</scope>
  6. </dependency>
英文:

Make sure you have the following dependency in your pom file

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.mockito&lt;/groupId&gt;
  3. &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
  4. &lt;version&gt;3.5.15&lt;/version&gt;
  5. &lt;scope&gt;test&lt;/scope&gt;
  6. &lt;/dependency&gt;

答案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

huangapple
  • 本文由 发表于 2020年10月21日 18:46:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64461915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定