Skip to content

Instantly share code, notes, and snippets.

@npryce
Last active Aug 29, 2015
Embed
What would you like to do?
How to create a Log4J Logger for unit testing Java code that logs
LoggingEvent loggedEvent = null;
private Logger capturingLogger() {
return new Logger("testing") {
{
level = Level.ALL;
repository = mock(LoggerRepository.class);
when(repository.isDisabled(anyInt())).thenReturn(false);
}
@Override
public void callAppenders(LoggingEvent event) {
assertNull("expected only one event", loggedEvent);
loggedEvent = event;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment