是否有可能以某种方式拦截日志记录(SLF4J + logback)并通过JUnit测试用例获取InputStream
(或其他可读的内容)......?
答案 0 :(得分:23)
您可以创建自定义附加程序
public class TestAppender extends AppenderBase<LoggingEvent> {
static List<LoggingEvent> events = new ArrayList<>();
@Override
protected void append(LoggingEvent e) {
events.add(e);
}
}
并配置logback-test.xml以使用它。现在我们可以从测试中检查记录事件:
@Test
public void test() {
...
Assert.assertEquals(1, TestAppender.events.size());
...
}
答案 1 :(得分:13)
Slf4j API没有提供这种方法,但是Logback提供了一种简单的解决方案。
您可以使用ListAppender
:一个白盒回传追加器,其中在public List
字段中添加了日志条目,我们可以使用该条目进行断言。
这是一个简单的例子。
Foo类:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Foo {
static final Logger LOGGER = LoggerFactory.getLogger(Foo .class);
public void doThat() {
logger.info("start");
//...
logger.info("finish");
}
}
FooTest类:
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
public class FooTest {
@Test
void doThat() throws Exception {
// get Logback Logger
Logger fooLogger = (Logger) LoggerFactory.getLogger(Foo.class);
// create and start a ListAppender
ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
listAppender.start();
// add the appender to the logger
fooLogger.addAppender(listAppender);
// call method under test
Foo foo = new Foo();
foo.doThat();
// JUnit assertions
List<ILoggingEvent> logsList = listAppender.list;
assertEquals("start", logsList.get(0)
.getMessage());
assertEquals(Level.INFO, logsList.get(0)
.getLevel());
assertEquals("finish", logsList.get(1)
.getMessage());
assertEquals(Level.INFO, logsList.get(1)
.getLevel());
}
}
您还可以将Matcher / assertion库用作AssertJ或Hamcrest。
使用AssertJ,它将是:
import org.assertj.core.api.Assertions;
Assertions.assertThat(listAppender.list)
.extracting(ILoggingEvent::getMessage, ILoggingEvent::getLevel)
.containsExactly(Tuple.tuple("start", Level.INFO), Tuple.tuple("finish", Level.INFO));
答案 2 :(得分:11)
您可以使用http://projects.lidalia.org.uk/slf4j-test/中的slf4j-test。 它通过它自己的slf4j api实现替换了整个logback slf4j实现,并提供了一个api来对记录事件进行断言。
示例:
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<classpathDependencyExcludes>
<classpathDependencyExcludes>ch.qos.logback:logback-classic</classpathDependencyExcludes>
</classpathDependencyExcludes>
</configuration>
</plugin>
</plugins>
</build>
public class Slf4jUser {
private static final Logger logger = LoggerFactory.getLogger(Slf4jUser.class);
public void aMethodThatLogs() {
logger.info("Hello World!");
}
}
public class Slf4jUserTest {
Slf4jUser slf4jUser = new Slf4jUser();
TestLogger logger = TestLoggerFactory.getTestLogger(Slf4jUser.class);
@Test
public void aMethodThatLogsLogsAsExpected() {
slf4jUser.aMethodThatLogs();
assertThat(logger.getLoggingEvents(), is(asList(info("Hello World!"))));
}
@After
public void clearLoggers() {
TestLoggerFactory.clear();
}
}
答案 3 :(得分:3)
尽管创建自定义的logback附加程序是一个很好的解决方案,但这只是第一步,您最终将最终开发/重新发明slf4j-test,而如果进一步,您将进一步开发:spf4j-slf4j-test或其他我还不知道的框架。
您最终将需要担心内存中保留了多少事件,在记录错误(且未声明错误)时使单元测试失败,使测试失败时可以使用调试日志,等等...
免责声明:我是spf4j-slf4j-test的作者,我写了此后端,以便能够更好地测试spf4j,在这里可以查看有关如何使用spf4j-slf4j-的示例。测试。我获得的主要优点之一是减少了构建输出(这是Travis所限制的),同时仍然具有发生故障时所需的所有细节。
答案 4 :(得分:2)
我在测试日志行时遇到了问题: LOGGER.error(消息,例外)。
http://projects.lidalia.org.uk/slf4j-test/中描述的解决方案也尝试在异常上断言,并且重新创建堆栈跟踪并不容易(在我看来毫无价值)。
我以这种方式解决了:
import org.junit.Test;
import org.slf4j.Logger;
import uk.org.lidalia.slf4jext.LoggerFactory;
import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
import static uk.org.lidalia.slf4jext.Level.ERROR;
import static uk.org.lidalia.slf4jext.Level.INFO;
public class Slf4jLoggerTest {
private static final Logger LOGGER = LoggerFactory.getLogger(Slf4jLoggerTest.class);
private void methodUnderTestInSomeClassInProductionCode() {
LOGGER.info("info message");
LOGGER.error("error message");
LOGGER.error("error message with exception", new RuntimeException("this part is not tested"));
}
private static final TestLogger TEST_LOGGER = TestLoggerFactory.getTestLogger(Slf4jLoggerTest.class);
@Test
public void testForMethod() throws Exception {
// when
methodUnderTestInSomeClassInProductionCode();
// then
assertThat(TEST_LOGGER.getLoggingEvents()).extracting("level", "message").contains(
tuple(INFO, "info message"),
tuple(ERROR, "error message"),
tuple(ERROR, "error message with exception")
);
}
}
这也是不依赖 Hamcrest matchers 库的优势。
答案 5 :(得分:1)
我建议使用一个简单,可重用的间谍实现,该实现可以作为JUnit规则包含在测试中:
[_ngContent-C0-myApp]
在测试中,您可以通过以下方式激活间谍:
public final class LogSpy extends ExternalResource {
private Logger logger;
private ListAppender<ILoggingEvent> appender;
@Override
protected void before() {
appender = new ListAppender<>();
logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); // cast from facade (SLF4J) to implementation class (logback)
logger.addAppender(appender);
appender.start();
}
@Override
protected void after() {
logger.detachAppender(appender);
}
public List<ILoggingEvent> getEvents() {
if (appender == null) {
throw new UnexpectedTestError("LogSpy needs to be annotated with @Rule");
}
return appender.list;
}
}
调用@Rule
public LogSpy log = new LogSpy();
(或其他自定义方法)以检查记录的事件。
答案 6 :(得分:1)
private ListAppender<ILoggingEvent> logWatcher;
@BeforeEach
void setup() {
this.logWatcher = new ListAppender<>();
this.logWatcher.start();
((Logger) LoggerFactory.getLogger(MyClass.class)).addAppender(this.logWatcher);
}
@Test
void myMethod_logs2Messages() {
...
int logSize = logWatcher.list.size();
assertThat(logWatcher.list.get(logSize - 2).getFormattedMessage()).contains("EXPECTED MSG 1");
assertThat(logWatcher.list.get(logSize - 1).getFormattedMessage()).contains("EXPECTED MSG 2");
}
功劳:@davidxxx的答案。参见import ch.qos.logback...
详细信息:https://stackoverflow.com/a/52229629/601844
答案 7 :(得分:0)
一个简单的解决方案可能是使用Mockito模拟附加器(例如)
@Slf4j
class MyClass {
public void doSomething() {
log.info("I'm on it!");
}
}
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
@Mock private Appender<ILoggingEvent> mockAppender;
private MyClass sut = new MyClass();
@Before
public void setUp() {
Logger logger = (Logger) LoggerFactory.getLogger(MyClass.class.getName());
logger.addAppender(mockAppender);
}
@Test
public void shouldLogInCaseOfError() {
sut.doSomething();
verify(mockAppender).doAppend(ArgumentMatchers.argThat(argument -> {
assertThat(argument.getMessage(), containsString("I'm on it!"));
assertThat(argument.getLevel(), is(Level.INFO));
return true;
}));
}
}
注意:我使用断言而不是返回false
作为make代码和(可能)错误更易于阅读。