mock log4j
private StringBuilder mockLog() {
final StringBuilder stringBuilder = new StringBuilder();
new MockUp<Log4jLogger>() {
@mockit.Mock
public void info(final String format, final Object o) {
ParameterizedMessage parameterizedMessage = new ParameterizedMessage(format, o);
stringBuilder.append(parameterizedMessage.getFormattedMessage());
}
@mockit.Mock
public void error(final String format) {
stringBuilder.append(format);
}
@mockit.Mock
public void error(final String format, final Object o) {
ParameterizedMessage parameterizedMessage = new ParameterizedMessage(format, o);
stringBuilder.append(parameterizedMessage.getFormattedMessage());
}
public void error(final String format, final Throwable t) {
ParameterizedMessage parameterizedMessage = new ParameterizedMessage(format, t);
stringBuilder.append(parameterizedMessage.getFormattedMessage());
}
};
return stringBuilder;
}
校验日志内容
final StringBuilder stringBuilder = mockLog();
xxx.run();
Assertions.assertEquals("xxxxx", stringBuilder.toString());
|