Why does Mockito show “mockStatic(Class<T>) is undefined” even after adding the correct dependency?

I’m trying to mock a static method using Mockito, but I keep getting the error: mockStatic(Class<FacesContext>) is undefined for the type TestMainPage.

I’ve already added the required dependencies for JUnit and Mockito in my Maven pom.xml, including:

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>5.14.2</version>
  <scope>test</scope>
</dependency>

However, Eclipse still reports the method as undefined in my test class. Could this be related to imports, Maven setup, or something else I’m missing?

The mockStatic() method isn’t part of mockito-core. You need to include the mockito-inline dependency. Add this to your pom.xml:

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-inline</artifactId>
  <version>5.14.2</version>
  <scope>test</scope>
</dependency>

After that, import it using import static org.mockito.Mockito.*; , Eclipse will then recognize mockStatic().

This happens because static mocking is not included in mockito-core; it’s part of the inline mocking extension.

To fix it:

  1. Add mockito-inline instead of or alongside mockito-core.

  2. Ensure your imports use org.mockito.Mockito.*.

Use JUnit 5 or JUnit 4.13+, older JUnit versions may conflict.

Once added, rebuild Maven (mvn clean test) and Eclipse will resolve mockStatic() correctly.

mockStatic() is available only when using the inline mock maker, which comes from the mockito-inline artifact.

The core version alone doesn’t include bytecode manipulation required for static mocking.

Add:

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-inline</artifactId>
  <version>5.14.2</version>
</dependency>

Then, use:

try (MockedStatic<FacesContext> context = Mockito.mockStatic(FacesContext.class)) {
    context.when(FacesContext::getCurrentInstance).thenReturn(mockInstance);
}

This approach works from Mockito 3.4.0 onward. This should solve your issue @Rashmihasija