public class CapturedLogging
extends org.junit.rules.ExternalResource
This implementation is Log4J specific, but the interface is framed in SLF4J terminology. The intent is that if we need to switch to a different logger we should be able to preserve the interface, since it mirrors the SLF4J interface used by non test code. The implementation uses a Log4j appender to capture log messages for verification.
Typical usage is to declare the rule
@Rule
public CapturedLogging capturedLogging = new CapturedLogging(ClassBeingTested.class);
and then use the provided matcher factories in tests
assertThat(capturedLogging, didLogWarn("expected log message", "expected text detail"));
| Constructor and Description |
|---|
CapturedLogging(Class logSource) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
after() |
protected void |
before() |
static org.hamcrest.Matcher<CapturedLogging> |
didLogDebug(org.hamcrest.Matcher<String> messageMatcher)
Obtain a
Matcher which matches a CapturedLogging if a message matching the given
messageMatcher was logged at Level.DEBUG. |
static org.hamcrest.Matcher<CapturedLogging> |
didLogDebug(String... substrings)
Obtain a
Matcher which matches a CapturedLogging if a single message containing all the given
substrings was logged at Level.DEBUG. |
static org.hamcrest.Matcher<CapturedLogging> |
didLogError(org.hamcrest.Matcher<String> messageMatcher)
Obtain a
Matcher which matches a CapturedLogging if a message matching the given
messageMatcher was logged at Level.ERROR. |
static org.hamcrest.Matcher<CapturedLogging> |
didLogError(String... substrings)
Obtain a
Matcher which matches a CapturedLogging if a single message containing all the given
substrings was logged at Level.ERROR. |
static org.hamcrest.Matcher<CapturedLogging> |
didLogInfo(org.hamcrest.Matcher<String> messageMatcher)
Obtain a
Matcher which matches a CapturedLogging if a message matching the given
messageMatcher was logged at Level.INFO. |
static org.hamcrest.Matcher<CapturedLogging> |
didLogInfo(String... substrings)
Obtain a
Matcher which matches a CapturedLogging if a single message containing all the given
substrings was logged at Level.INFO. |
static org.hamcrest.Matcher<CapturedLogging> |
didLogWarn(org.hamcrest.Matcher<String> messageMatcher)
Obtain a
Matcher which matches a CapturedLogging if a message matching the given
messageMatcher was logged at Level.WARN. |
static org.hamcrest.Matcher<CapturedLogging> |
didLogWarn(String... substrings)
Obtain a
Matcher which matches a CapturedLogging if a single message containing all the given
substrings was logged at Level.WARN. |
String |
toString() |
public CapturedLogging(Class logSource)
protected void before()
throws Throwable
before in class org.junit.rules.ExternalResourceThrowableprotected void after()
after in class org.junit.rules.ExternalResourcepublic static org.hamcrest.Matcher<CapturedLogging> didLogError(org.hamcrest.Matcher<String> messageMatcher)
Matcher which matches a CapturedLogging if a message matching the given
messageMatcher was logged at Level.ERROR.messageMatcher - the matcher for the log message.public static org.hamcrest.Matcher<CapturedLogging> didLogError(String... substrings)
Matcher which matches a CapturedLogging if a single message containing all the given
substrings was logged at Level.ERROR.substrings - the substrings required in the log message.public static org.hamcrest.Matcher<CapturedLogging> didLogWarn(org.hamcrest.Matcher<String> messageMatcher)
Matcher which matches a CapturedLogging if a message matching the given
messageMatcher was logged at Level.WARN.messageMatcher - the matcher for the log message.public static org.hamcrest.Matcher<CapturedLogging> didLogWarn(String... substrings)
Matcher which matches a CapturedLogging if a single message containing all the given
substrings was logged at Level.WARN.substrings - the substrings required in the log message.public static org.hamcrest.Matcher<CapturedLogging> didLogInfo(org.hamcrest.Matcher<String> messageMatcher)
Matcher which matches a CapturedLogging if a message matching the given
messageMatcher was logged at Level.INFO.messageMatcher - the matcher for the log message.public static org.hamcrest.Matcher<CapturedLogging> didLogInfo(String... substrings)
Matcher which matches a CapturedLogging if a single message containing all the given
substrings was logged at Level.INFO.substrings - the substrings required in the log message.public static org.hamcrest.Matcher<CapturedLogging> didLogDebug(org.hamcrest.Matcher<String> messageMatcher)
Matcher which matches a CapturedLogging if a message matching the given
messageMatcher was logged at Level.DEBUG.messageMatcher - the matcher for the log message.public static org.hamcrest.Matcher<CapturedLogging> didLogDebug(String... substrings)
Matcher which matches a CapturedLogging if a single message containing all the given
substrings was logged at Level.DEBUG.substrings - the substrings required in the log message.Copyright © 2017 Atlassian. All rights reserved.