Skip to content

Instantly share code, notes, and snippets.

@npryce
Last active August 29, 2015 14:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npryce/efb0437313ebae85b47d to your computer and use it in GitHub Desktop.
Save npryce/efb0437313ebae85b47d to your computer and use it in GitHub Desktop.
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