DebugLogger { public void debug(final Logger logger, final Supplier<String> message) { if (logger.isDebugEnabled()) { try { logger.debug(message.get()); } catch (final Exception e) { logger.error("Could not generate debug log message", e); } } }  void debug(final Logger logger, final Supplier<String> message);  }
@Test public void shouldLogNameIfDebugIsTrue() { when(logger.isDebugEnabled()).thenReturn(true); debugLogger.debug(logger, () -> "Test Message"); verify(logger).debug("Test Message"); }
@Test public void shouldNotLogNameIfDebugIsFalse() { when(logger.isDebugEnabled()).thenReturn(false); debugLogger.debug(logger, () -> "Test Message"); verify(logger).isDebugEnabled(); verifyNoMoreInteractions(logger); }
@Test public void shouldLogErrorIfExceptionIsThrownWhenLogging() { final RuntimeException runtimeException = new RuntimeException(); when(logger.isDebugEnabled()).thenReturn(true); debugLogger.debug(logger, () -> { throw runtimeException; }); verify(logger).isDebugEnabled(); verify(logger).error("Could not generate debug log message", runtimeException); }