UnnecessaryStubbingException when mocking LoggerFactory for isDebugEnabled

97 views Asked by At

I am trying to improve my branch coverage and mocking Logger to check both cases of "isDebugEnabled".

Below is my test case:

@Test
    public void testLogger() {
        try (MockedStatic<LoggerFactory> 
                loggerFactoryMockedStatic = mockStatic(LoggerFactory.class)) {
            Logger logger = mock(Logger.class);
            loggerFactoryMockedStatic.when(() -> LoggerFactory.getLogger(any(Class.class))).thenReturn(logger);
            
            when(logger.isDebugEnabled()).thenReturn(Boolean.FALSE);
            Response res = endpoint.check();
        }
        
    }

but it is resulting in "UnnecessaryStubbingException" when i am setting the debug enabled.

Any idea how can I get it done ?

1

There are 1 answers

0
MJG On

Enable the debug logging accordingly for testing configuration.

You could consider to run all the tests with all available logging levels for warning-/info-/trace-enabled, etc separately, perhaps as part of extended testing. Then combine all the log-results and coverage for reporting.