Mockito的mockStatic无法解析符号。

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

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;

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

我的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:

		&lt;dependency&gt;
			&lt;groupId&gt;org.mockito&lt;/groupId&gt;
			&lt;artifactId&gt;mockito-inline&lt;/artifactId&gt;
			&lt;version&gt;3.5.15&lt;/version&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.mockito&lt;/groupId&gt;
			&lt;artifactId&gt;mockito-junit-jupiter&lt;/artifactId&gt;
			&lt;version&gt;3.5.15&lt;/version&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;
        &lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
			&lt;version&gt;2.2.6.RELEASE&lt;/version&gt;
		&lt;/dependency&gt;
        &lt;dependency&gt;
			&lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
			&lt;artifactId&gt;spring-security-test&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&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:

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:
Mockito的mockStatic无法解析符号。

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

		&lt;dependency&gt;
            &lt;groupId&gt;org.mockito&lt;/groupId&gt;
            &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
            &lt;version&gt;3.5.15&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &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:

确定