I am using private static final LOGGER field in my class and I want LOGGER.isInfoEnabled() method to return false. How can
One way is using reflection get rid of final
modifier from the field and then replace the LOGGER
field with Mocked one
public class Class1Test {
@Test
public void test() throws Exception {
Logger logger = Mockito.mock(Logger.class);
Mockito.when(logger.isInfoEnabled()).thenReturn(false);
setFinalStatic(Class1.class.getDeclaredField("LOGGER"), logger);
Class1 cls1 = new Class1();
assertFalse(cls1.demoMethod());
}
static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
}
The accepted solution shouldn't work with JDK 12. The reason can be seen here.
It is easy to do it using PowerMockito (tested with version 2.0.9). You can use the Whitebox.setInternalState method to do it for you.
Example:
Whitebox.setInternalState(MyTestClass.class, "myCar", carMock);
MyTestClass is the class containing the field.
myCar is the variable name of the field.
carMock is some mock you want to pass.
Changing "@Mocked Logger" to "@Capturing Logger" in the parameter makes it work. like
@Test
public void test(@Capturing final Logger LOGGER) {
new NonStrictExpectations() {
{
LOGGER.isInfoEnabled();
result = false;
}
};
assertFalse(cls1.demoMethod());
}
I think mockito or jMockit cant mock static final classes as they try to override the methods while unit testing. However, powerMockito can as it uses Reflection and mocks the static final class/methods.