org.xwiki.test
Class LogRule
java.lang.Object
org.xwiki.test.LogRule
- All Implemented Interfaces:
- org.junit.rules.TestRule
public class LogRule
- extends Object
- implements org.junit.rules.TestRule
Allow capturing Logs output in the Class under test during unit testing. This is useful for two reasons:
- it allows not outputting log messages in the console which is a bad practice. When a test run it should not
output anything and if it needs to assert something, it has to be done in the test itself.
- it allows to assert the output log messages
This code was inspired by code written by Christian Baranowski in a
blog post.
Example usage:
@Rule public LogRule logRule = new LogRule() {{
record(LogLevel.WARN);
recordLoggingForType(RestrictParseLocationEventHandler.class);
}};
- Since:
- 4.2RC1
- Version:
- $Id: 8f56c07f8560fc0eab2a4a26cf82bc284e7179f6 $
|
Nested Class Summary |
static class |
LogRule.LogLevel
Helper class to represent Logging levels to capture. |
class |
LogRule.LogStatement
The actual code that executes our capturing logic before the test runs and removes it after it has run. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
LogRule
public LogRule()
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement statement,
org.junit.runner.Description description)
- Specified by:
apply in interface org.junit.rules.TestRule
record
public void record(LogRule.LogLevel level)
- Parameters:
level - the log level to capture
recordLoggingForType
public void recordLoggingForType(Class<?> type)
- Parameters:
type - the logging class type for which to capture logs
contains
public boolean contains(String logOutput)
- Parameters:
logOutput - the log output to match in the captured data
- Returns:
- true if the passed text was logged or false otherwise
getMessage
public String getMessage(int position)
- Parameters:
position - the message number in the list of captured logs
- Returns:
- the message at the specified position
size
public int size()
- Returns:
- the number of log messages that have been captured
Copyright © 2004–2015 XWiki. All rights reserved.