A JUnit5 extension to assist monitoring Logback log events and elaborate tests to confirm that events were logged.
import com.figtreelake.spyrootlogger.SpyRootLogger;
import com.figtreelake.spyrootlogger.SpyRootLoggerExtension;
import com.figtreelake.spyrootlogger.SpyRootLoggerInject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import java.util.regex.Pattern;
@ExtendWith(SpyRootLoggerExtension.class)
class FooTest {
private Foo foo;
@SpyRootLoggerInject
private SpyRootLogger spyRootLogger;
@BeforeEach
void setUp() {
foo = new Foo();
}
@Test
void shouldLogExceptionWhenHandlingRuntimeException() {
final var exception = new RuntimeException("Dummy exception");
final var responseEntity = foo.handleException(exception);
/* Spy Root Logger can be used to check if there is a log event with
a specific exception attached. */
assertThat(spyRootLogger.findEventsByThrowableAttached(exception))
.isNotEmpty();
}
@Test
void shouldLogErrorEventWhenHandlingIllegalStateException() {
final var mockedIllegalStateException = mock(IllegalStateException.class);
final var responseEntity = foo.handleException(mockedIllegalStateException);
/* Spy Root Logger can be used to check how many events of such level were
logged */
assertThat(spyRootLogger.countErrorEvents()).isPositive();
}
@Test
void shouldLogMessageInformingMessageNotReadableWhenHandlingHttpMessageNotReadableException() {
final var mockedHttpMessageNotReadableException = mock(HttpMessageNotReadableException.class);
final var messagePattern = Pattern.compile("^Failed to read HTTP message");
final var responseEntity = foo.handleException(mockedHttpMessageNotReadableException);
/* Spy Root Logger can be used to find log events by regular expressions
on its message */
assertThat(spyRootLogger.findEventsByMessagePattern(messagePattern))
.isNotEmpty();
}
}
Note: Functional examples can be found on Spy Root Logger Example repository.
-
Be sure to implement your tests using JUnit5 framework.
-
Add Spy Root Logger on your project.
-
For Maven projects add the following on your
pom.xml
file under<dependencies>
tag.<dependency> <groupId>com.figtreelake</groupId> <artifactId>spy-root-logger</artifactId> <version>1.0</version> </dependency>
-
For Gradle projects add the following on your
build.gradle
file underdependencies
declaration.implementation 'com.figtreelake:spy-root-logger:1.0'
-
You can check the latest version available on Maven Central repository.
-
-
Annotate your test class with
@ExtendWith
informingSpyRootLoggerExtension
class as value. -
Add a
SpyRootLogger
field and annotate it with@SpyRootLoggerInject
.import com.figtreelake.spyrootlogger.SpyRootLogger; import com.figtreelake.spyrootlogger.SpyRootLoggerExtension; import com.figtreelake.spyrootlogger.SpyRootLoggerInject; import org.junit.jupiter.api.extension.ExtendWith; @ExtendWith(SpyRootLoggerExtension.class) class FooTest { @SpyRootLoggerInject private SpyRootLogger spyRootLogger; /* Test declarations... */ }
Try out the library. If you like the outcome, give a star for its repository, share or talk about it with your IT friends and colleagues. This is a work I have been doing in my spare time and I really would like to see that people appreciate the time I have invested on it.
If you liked the project and really want to demonstrate your appreciation, you can send me a "thank you" coffee. 🙂