Mock private static final field using mockito or Jmockit

前端 未结 4 1120
感情败类
感情败类 2020-12-05 09:31

I am using private static final LOGGER field in my class and I want LOGGER.isInfoEnabled() method to return false. How can

相关标签:
4条回答
  • 2020-12-05 09:50

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-05 09:51

    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.

    0 讨论(0)
  • 2020-12-05 09:53

    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());
      }
    
    0 讨论(0)
  • 2020-12-05 10:14

    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.

    0 讨论(0)
提交回复
热议问题