6

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 ?

Ehrakis
  • 247
  • 2
  • 4
  • 9

2 Answers2

17

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>
Enrico Trentin
  • 272
  • 4
  • 7
0

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

Abdullah Ismail
  • 415
  • 5
  • 6
  • Thank you for your answer, the question was about the Mockito framework which is different from PowerMockito. – Ehrakis May 26 '22 at 13:30