Mockito: mockStatic is underfined

No matter what I try I get the error "mockStatic(Class<FacesContext>) is undefined for the type TestMainPage". I've tried several differnet combinations of dependencies. I'm using Eclipse and Maven. Here is what the dependency section of my POM currently looks like:
<!-- JUnit for tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- test dependencies -->
<dependency>
<!-- Required to mock static methods like FacesContext.getCurrentInstance() -->
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.14.2</version>
<scope>test</scope>
</dependency>
<!-- JUnit for tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- test dependencies -->
<dependency>
<!-- Required to mock static methods like FacesContext.getCurrentInstance() -->
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.14.2</version>
<scope>test</scope>
</dependency>
Here is my test code:
import javax.faces.context.FacesContext;

import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import static org.mockito.Mockito.*;

public class TestMainPage {

@Rule
public MockitoRule rule = MockitoJUnit.rule();

@Mock
private FacesContext mockedFacesContext;

@Test
public void testView() {
try (MockedStatic<FacesContext> mockedStaticFacesContext = mockStatic(FacesContext.class)) {
mockedStaticFacesContext.when(() -> FacesContext.getCurrentInstance()).thenReturn(mockedFacesContext);

ViewBean.run();
}
}
}
import javax.faces.context.FacesContext;

import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import static org.mockito.Mockito.*;

public class TestMainPage {

@Rule
public MockitoRule rule = MockitoJUnit.rule();

@Mock
private FacesContext mockedFacesContext;

@Test
public void testView() {
try (MockedStatic<FacesContext> mockedStaticFacesContext = mockStatic(FacesContext.class)) {
mockedStaticFacesContext.when(() -> FacesContext.getCurrentInstance()).thenReturn(mockedFacesContext);

ViewBean.run();
}
}
}
What am I missing? Could this be an Eclipse issue, a Maven issue, a Build Path issue? What else should I try?
3 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @AgentMime! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
AgentMime
AgentMimeOP3w ago
Never mind, I think I fixed it. One of the other projects was using a different version of Maven and that was causing problems for some reason
JavaBot
JavaBot3w ago
Post Closed
This post has been closed by <@406648669748592650>.
Want results from more Discord servers?
Add your server