Logsert helps to test logging functionality by recording log-events during test-runs and providing easy assertions.
<dependency>
<groupId>com.github.nylle</groupId>
<artifactId>logsert</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
class SomethingThatLogsTest {
@RegisterExtension
LogRecorder logRecorder = new LogRecorder(SomethingThatLogs.class);
@Test
void fluentAssertionsAreConvenient() {
var sut = new SomethingThatLogs();
var expectedException = new RuntimeException("expected for test");
sut.logInfoWithMdcAndException("message", Map.of("key", "value", "foo", "bar"), expectedException);
assertThat(logRecorder).containsLogs(1)
.withMessage("message")
.withMessageContaining("essa")
.withLevel(Level.INFO)
.withMdcEntry("foo", "bar")
.withMdcEntry("key", "value")
.withMdcEntries(Map.of("key", "value"))
.withMdcEntries(Map.of("foo", "bar"))
.withMdcEntriesExactly(Map.of("key", "value", "foo", "bar"))
.withException(expectedException)
.withException(RuntimeException.class)
.withException(RuntimeException.class, "expected for test");
}
@Test
void standardListAssertionsAreAlsoSupported() {
var sut = new SomethingThatLogs();
sut.logInfoWithMdcAndException("message", Map.of("key", "value"), new RuntimeException("expected for test"));
assertThat(logRecorder.getLogEvents())
.extracting("level", "message", "MDCPropertyMap", "throwableProxy.className", "throwableProxy.message")
.contains(tuple(Level.INFO, "message", Map.of("key", "value"), RuntimeException.class.getName(), "expected for test"));
}
}