'Mockito mockStatic cannot resolve symbol

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:
enter image description here

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



Solution 1:[1]

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>

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Enrico Trentin
Solution 2 Abdullah Ismail