【发布时间】:2022-01-02 11:03:01
【问题描述】:
如何为在服务或控制器类的方法中编写的 Log 语句编写 Junit 测试用例。
【问题讨论】:
标签: java spring-boot junit microservices junit5
如何为在服务或控制器类的方法中编写的 Log 语句编写 Junit 测试用例。
【问题讨论】:
标签: java spring-boot junit microservices junit5
您可以尝试OutputCaptureExtension,它将捕获System.out 和System.err 打印出的所有内容。一个例子是:
@ExtendWith(OutputCaptureExtension.class)
public class MyTest {
@Test
public void theTest(CapturedOutput output) {
FooService fooService = new FooService();
fooService.doSomthing();
assertThat(output.getOut()).contains("bar");
}
}
然后它会期望fooService.doSomthing() 会打印出一些包含单词“bar”的日志语句到System.out
【讨论】: