【问题标题】:How to write Junit Test case for particular log statement in a particular Method [duplicate]如何为特定方法中的特定日志语句编写 Junit 测试用例 [重复]
【发布时间】:2022-01-02 11:03:01
【问题描述】:

如何为在服务或控制器类的方法中编写的 Log 语句编写 Junit 测试用例。

【问题讨论】:

    标签: java spring-boot junit microservices junit5


    【解决方案1】:

    您可以尝试OutputCaptureExtension,它将捕获System.outSystem.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

    【讨论】:

      猜你喜欢
      • 2016-12-16
      • 2023-02-17
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      相关资源
      最近更新 更多