如何使用 junit 断言 org.slf4j.Logger 语句?
如何在 spring-boot-test
中使用 junit
验证来自 org.slf4j.Logger
的日志语句?
@Service
public class MyService {
private final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(this.getClass());
public void run() {
LOGGER.info("running");
}
}
@SpringBootTest
public class MyTest {
@Autowired
private MyService service;
@Test
public void test() {
service.run();
//TODO how to validate log statement?
}
}
How can I validate log statements from a org.slf4j.Logger
with junit
in a spring-boot-test
?
@Service
public class MyService {
private final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(this.getClass());
public void run() {
LOGGER.info("running");
}
}
@SpringBootTest
public class MyTest {
@Autowired
private MyService service;
@Test
public void test() {
service.run();
//TODO how to validate log statement?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了 Springs
@ExtendWith(OutputCaptureExtension.class)
的解决方案:它需要 log4j 配置上的
follow="true"
选项:I found a solution with Springs
@ExtendWith(OutputCaptureExtension.class)
:It requires the
follow="true"
option on your log4j config:<Console name="CONSOLE" target="SYSTEM_OUT" follow="true">
如果底层 SLF4J 是 Log4j 2.x,正如您的标签可能暗示的那样,您可以使用
ListAppender
,其中包含在log4j-core
的test-jar
中。If the underlying SLF4J is Log4j 2.x, as your tags might implicate, you can use a
ListAppender
, which is contained in thelog4j-core
'stest-jar
.