Class ExpectedLogMessagesRule
- All Implemented Interfaces:
org.junit.rules.TestRule
public final class ExpectedLogMessagesRule extends Object implements org.junit.rules.TestRule
-
Constructor Summary
Constructors Constructor Description ExpectedLogMessagesRule()
-
Method Summary
Modifier and Type Method Description org.junit.runners.model.Statement
apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
void
expectErrorsForTag(String tag)
Blanket suppress test failures due to errors from a tag.void
expectLogMessage(int level, String tag, String message)
Adds an expected log statement.void
expectLogMessagePattern(int level, String tag, Pattern messagePattern)
Adds an expected log statement using a regular expression.void
expectLogMessageWithThrowable(int level, String tag, String message, Throwable throwable)
Adds an expected log statement with extra check ofThrowable
.void
expectLogMessageWithThrowableMatcher(int level, String tag, String message, org.hamcrest.Matcher<Throwable> throwableMatcher)
Adds an expected log statement with extra check ofMatcher
.void
ignoreMissingLoggedTags(boolean shouldIgnore)
If set true, tests that callexpectErrorsForTag(String)
but do not log errors for the given tag will not fail.
-
Constructor Details
-
ExpectedLogMessagesRule
public ExpectedLogMessagesRule()
-
-
Method Details
-
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)- Specified by:
apply
in interfaceorg.junit.rules.TestRule
-
expectLogMessage
Adds an expected log statement. If this log is not printed during test execution, the test case will fail.This will also match any log statement which contain a throwable as well. For verifying the throwable, please see
expectLogMessageWithThrowable(int, String, String, Throwable)
.Do not use this to suppress failures. Use this to test that expected error cases in your code cause log messages to be printed.
-
expectLogMessagePattern
Adds an expected log statement using a regular expression. If this log is not printed during test execution, the test case will fail. When possible, log output should be made determinstic andexpectLogMessage(int, String, String)
used instead.This will also match any log statement which contain a throwable as well. For verifying the throwable, please see
expectLogMessageWithThrowable(int, String, String, Throwable)
.Do not use this to suppress failures. Use this to test that expected error cases in your code cause log messages to be printed.
-
expectLogMessageWithThrowable
public void expectLogMessageWithThrowable(int level, String tag, String message, Throwable throwable)Adds an expected log statement with extra check ofThrowable
. If this log is not printed during test execution, the test case will fail. Do not use this to suppress failures. Use this to test that expected error cases in your code cause log messages to be printed. -
expectLogMessageWithThrowableMatcher
public void expectLogMessageWithThrowableMatcher(int level, String tag, String message, org.hamcrest.Matcher<Throwable> throwableMatcher)Adds an expected log statement with extra check ofMatcher
. If this log is not printed during test execution, the test case will fail. Do not use this to suppress failures. Use this to test that expected error cases in your code cause log messages to be printed. -
expectErrorsForTag
Blanket suppress test failures due to errors from a tag. If this tag is not printed at Log.ERROR during test execution, the test case will fail (unlessignoreMissingLoggedTags(boolean)
is used).Avoid using this method when possible. Prefer to assert on the presence of a specific message using
expectLogMessage(int, java.lang.String, java.lang.String)
in test cases that *intentionally* trigger an error. -
ignoreMissingLoggedTags
public void ignoreMissingLoggedTags(boolean shouldIgnore)If set true, tests that callexpectErrorsForTag(String)
but do not log errors for the given tag will not fail. By default this is false.Avoid using this method when possible. Prefer tests that print (or do not print) log messages deterministically.
-