Mock private static final field using mockito or Jmockit

前端 未结 4 1128
感情败类
感情败类 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: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.

提交回复
热议问题