Logger.log(LogRecord) must be expected by the
test case being evaluated, and that all expected log events must
occur in order.
This mechanism is tied in automatically when depending on
iu-java-test for unit testing. Any use of
Logger.log(LogRecord) will cause the test to fail unless the log
event is explicitly expected to be the next log event. Log event expectations
require:
- Logger name to match exactly
- Log level to match exactly
- Log message to match a
regular expression - Thrown exception class to match exactly, or for an exception to not be thrown
- Thrown exception to meet additional criteria if defined by
Predicate
Platform-level loggers are exempt, and will be logged
normally as configured by java.logging before the test framework is
configured, i.e., INFO level and higher logged to console. The following
logger names are considered platform loggers.
- org.junit
- org.mockito
- org.apiguardian
- net.bytebuddy
- org.objenesis
- org.opentest4j
- Any logger name for which
IuObject.isPlatformName(String)returns true - Any logger name prefixed by the comma-separated
test property valueiu.util.test.platformLoggers
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidAllows a log messages from a logger.static voidAllows a log message with or without an exception.static voidAllows a log message with a thrown exception.static <T extends Throwable>
voidallow(String loggerName, Level level, String message, Class<T> thrownClass, Predicate<T> thrownTest) Allows a log message with a thrown exception.static voidAsserts that all expected messages declared so far have been logged.(package private) static voidclearUnexpected(Throwable unexpected) Clears an unexpected log message from the pending queue.(package private) static voiddestroy()Test destroy hook.static voidExpects a log message with no thrown exception.static voidExpects a log message with a thrown exception.static <T extends Throwable>
voidexpect(String loggerName, Level level, String message, Class<T> thrownClass, Predicate<T> thrownTest) Expects a log message with a thrown exception.(package private) static voidfinishTest(String name) Test finish hook.(package private) static voidinit()Initialization hook.(package private) static booleanisPlatformLogger(String loggerName) Determines if a logger name is related to a platform logger, and so should omitted from test expectations.(package private) static voidTest start hook.
-
Method Details
-
isPlatformLogger
Determines if a logger name is related to a platform logger, and so should omitted from test expectations.- Parameters:
loggerName- logger name- Returns:
- true if name is associated with a platform logger
-
init
static void init()Initialization hook. -
startTest
Test start hook.- Parameters:
name- test name
-
finishTest
Test finish hook.- Parameters:
name- test name
-
destroy
static void destroy()Test destroy hook. -
allow
Allows a log messages from a logger.Messages may be logged zero or more times, and will be exempt from expectation checks.
- Parameters:
loggerName- Logger name prefixlevel- maximum log level to allow
-
allow
Allows a log message with or without an exception.The message may be logged zero or more times, and will be exempt from expectation checks.
- Parameters:
loggerName- Logger name prefixlevel- maximum log level to allowmessage- regular expression to match against the message
-
allow
public static void allow(String loggerName, Level level, String message, Class<? extends Throwable> thrownClass) Allows a log message with a thrown exception.The message may be logged zero or more times, and will be exempt from expectation checks.
- Parameters:
loggerName- Logger name prefixlevel- maximum log level to allowmessage- regular expression to match against the messagethrownClass- Expected exception class, must match exactly
-
allow
public static <T extends Throwable> void allow(String loggerName, Level level, String message, Class<T> thrownClass, Predicate<T> thrownTest) Allows a log message with a thrown exception.The message may be logged zero or more times, and will be exempt from expectation checks.
- Type Parameters:
T- Thrown exception type- Parameters:
loggerName- Logger name prefixlevel- maximum log level to allowmessage- regular expression to match against the messagethrownClass- Expected exception class, must match exactlythrownTest- Expected exception test
-
expect
Expects a log message with no thrown exception.- Parameters:
loggerName- Logger name, must match exactlylevel- level, must match exactlymessage- regular expression to match against the message
-
expect
public static void expect(String loggerName, Level level, String message, Class<? extends Throwable> thrownClass) Expects a log message with a thrown exception.- Parameters:
loggerName- Logger name, must match exactlylevel- level, must match exactlymessage- regular expression to match against the messagethrownClass- Expected exception class, must match exactly
-
expect
public static <T extends Throwable> void expect(String loggerName, Level level, String message, Class<T> thrownClass, Predicate<T> thrownTest) Expects a log message with a thrown exception.- Type Parameters:
T- Thrown exception type- Parameters:
loggerName- Logger name, must match exactlylevel- level, must match exactlymessage- regular expression to match against the messagethrownClass- Expected exception class, must match exactlythrownTest- Expected exception test
-
assertExpectedMessages
public static void assertExpectedMessages()Asserts that all expected messages declared so far have been logged. -
clearUnexpected
Clears an unexpected log message from the pending queue.- Parameters:
unexpected- error related to an unexpected log message
-