AgentMime
AgentMime
JCHJava Community | Help. Code. Learn.
Created by AgentMime on 12/4/2024 in #java-help
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?
5 replies
JCHJava Community | Help. Code. Learn.
Created by AgentMime on 9/18/2024 in #java-help
Java Hibernate with Negative Enums
I'm using Hibernate for JPA for persisting objects to my database. One class has four possible statuses that I want to represent with an enum, and I want to use a negative status for DELETED for consistency with previous classes:
public enum Status {
PENDING (0)
APPROVED(1)
DECLINED(2)
DELETED(-1)

private int numVal;

Status(int numVal) {
this.numVal = numVal;
}

public int getNumVal() {
return numVal;
}
}
public enum Status {
PENDING (0)
APPROVED(1)
DECLINED(2)
DELETED(-1)

private int numVal;

Status(int numVal) {
this.numVal = numVal;
}

public int getNumVal() {
return numVal;
}
}
However, Hibernate doesn't like this and is throwing an exception:
...
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 4
at [email protected]//org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor.fromOrdinal(EnumJavaTypeDescriptor.java:76)
at [email protected]//org.hibernate.metamodel.model.convert.internal.OrdinalEnumValueConverter.toDomainValue(OrdinalEnumValueConverter.java:38)
...
...
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 4
at [email protected]//org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor.fromOrdinal(EnumJavaTypeDescriptor.java:76)
at [email protected]//org.hibernate.metamodel.model.convert.internal.OrdinalEnumValueConverter.toDomainValue(OrdinalEnumValueConverter.java:38)
...
Is there a simple way to tell Hibernate to allow the negative Enum value? Or do I have to use non-negative values?
13 replies