Mockito.mockConstruction provides null logger in the tested class

72 views
Skip to first unread message

ashish chauhan

unread,
Aug 17, 2021, 11:08:48 AM8/17/21
to mockito
I have a Utility Class where a Logger is initialized in the following way:

 private static Logger log = LoggerBean.getLoggerBean().getLogger(Utility.class);

I want to test the sum Method in the Utility class which uses the above log.

The call to LoggerBean.getLoggerBean() will create an instance of LoggerBean and obliviously the default constructor of  LoggerBean is called here which has some strange code and must be avoided during the unit test.

In order to mock the above Logger in Utility class I have taken two approaches 1) by mocking the  LoggerBean constructor 2) by mocking the static call to  LoggerBean.getLoggerBean().

In both the approaches I get the log as null in the Utility class.

I have simulated this situation in a very small attached project.

Please help and guide how to get a non null log inside the Utility class without refactoring the code.
fun.zip
Reply all
Reply to author
Forward
0 new messages