jeudi 20 août 2015

JUnit: test that no error is logged

I am trying to test something like this:

try {
     logger.info("message");
     //do something
} catch(Exception e) {
     logger.error(errorMessage);
}

I know that it's not a good practice to catch an Exception, but there is some legacy code and there is no time for refactoring.

So, I write an unit test so that a NullPointerException will be thrown inside try block, but now I don't know how to write the assert line(obviously, unit test have to fail all the time). Please notice that I can`t use:

        final Logger logger = LogManager.getLogger(AnaliticsService.class);
        final Appender mockAppender = mock(Appender.class);
        logger.addAppender(mockAppender);
        final ArgumentCaptor<LoggingEvent> captor = ArgumentCaptor.forClass(LoggingEvent.class);
        Log4jConfigHelper.getInstance().bufferConfiguration();
        verify(mockAppender, times(x)).doAppend(captor.capture());

because I don`t know how many messages are logged when UT is running.

Aucun commentaire:

Enregistrer un commentaire