assertj-logging's intention is to provide an easy way to unit test expected logging for different logging implementations with JUnit and AssertJ.
Starting with version 0.5.0 a JUnit 5 extension is provided and JUnit 4 is no longer supported.
Versions up to 0.4.X provide a JUnit 4 @Rule
.
It currently supports
build.gradle
dependencies {
testImplementation 'de.neuland-bfi:assertj-logging-log4j:0.4.0'
}
pom.xml
<dependency>
<groupId>de.neuland-bfi</groupId>
<artifactId>assertj-logging-log4j</artifactId>
<version>0.5.0</version>
<scope>test</scope>
</dependency>
assert-logging provides a JUnit rule that adds an appender to capture log messages to the logger of the logging source (the class emitting log messages).
After execution of the code under test this rule can be fed to a set of AssertJ assertions to verify that the expected logging was emitted.
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import de.neuland.assertj.logging.ExpectedLogging;
import static de.neuland.assertj.logging.ExpectedLoggingAssertions.assertThat;
public class LoggingSourceTest {
@RegisterExtension
private final ExpectedLogging logging = ExpectedLogging.forSource(LoggingSource.class);
@Test
void shouldCaptureLogging() {
// given
String expectedMessage = "Error Message";
// when
new LoggingSource().doSomethingThatLogsErrorMessage();
// then
assertThat(logging).hasErrorMessage(expectedMessage);
}
}
import org.junit.Rule;
import org.junit.Test;
import de.neuland.assertj.logging.ExpectedLogging;
import static de.neuland.assertj.logging.ExpectedLoggingAssertions.assertThat;
public class LoggingSourceTest {
@Rule
public ExpectedLogging logging = ExpectedLogging.forSource(LoggingSource.class);
@Test
public void shouldCaptureLogging() {
// given
String expectedMessage = "Error Message";
// when
new LoggingSource().doSomethingThatLogsErrorMessage();
// then
assertThat(logging).hasErrorMessage(expectedMessage);
}
}
We consider ERROR, WARNING and INFO to be the test-worthy log levels.
Thus, assertj-logging provides assertions for these log levels.
The following assertions are available:
assertThat(logging).hasErrorMessage(String message);
assertThat(logging).hasNoErrorMessage();
assertThat(logging).hasErrorMessage(String message, Throwable throwable);
assertThat(logging).hasErrorMessageMatching(String regex);
assertThat(logging).hasErrorMessageMatching(String regex, Throwable throwable);
assertThat(logging).hasWarningMessage(String message);
assertThat(logging).hasNoWarningMessage();
assertThat(logging).hasWarningMessage(String message, Throwable throwable);
assertThat(logging).hasWarningMessageMatching(String regex);
assertThat(logging).hasWarningMessageMatching(String regex, Throwable throwable);
assertThat(logging).hasInfoMessage(String message);
assertThat(logging).hasNoInfoMessage();
assertThat(logging).hasInfoMessageMatching(String regex);
All assertions from AssertJ's ListAssert are available as well, e.g.
assertThat(logging).isEmpty();