SpringBootTest 和 MockMvc 创建了大量的控制台日志记录 - 如何禁用?

Cra*_*tis 5 java logging spring spring-test spring-boot

我们使用@SpringBootTest一个@Autowired private MockMvc mockMvc属性来模拟对我们的控制器类的 HTTP 请求。

出于某种我们似乎无法确定的原因,这为每个测试用例创建了大量日志记录,并且正在用数千行文本填充我们的日志,如下所示:

build   18-May-2019 03:09:40    Async:
build   18-May-2019 03:09:40        Async started = false
build   18-May-2019 03:09:40         Async result = null
build   18-May-2019 03:09:40    
build   18-May-2019 03:09:40    Resolved Exception:
build   18-May-2019 03:09:40                 Type = null
build   18-May-2019 03:09:40    
build   18-May-2019 03:09:40    ModelAndView:
build   18-May-2019 03:09:40            View name = null
build   18-May-2019 03:09:40                 View = null
build   18-May-2019 03:09:40                Model = null
build   18-May-2019 03:09:40    
build   18-May-2019 03:09:40    FlashMap:
build   18-May-2019 03:09:40           Attributes = null
build   18-May-2019 03:09:40    
build   18-May-2019 03:09:40    MockHttpServletResponse:
build   18-May-2019 03:09:40               Status = 200
build   18-May-2019 03:09:40        Error message = null
build   18-May-2019 03:09:40              Headers = {X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
build   18-May-2019 03:09:40         Content type = null
build   18-May-2019 03:09:40                 Body = 
build   18-May-2019 03:09:40        Forwarded URL = null
build   18-May-2019 03:09:40       Redirected URL = null
build   18-May-2019 03:09:40              Cookies = []
build   18-May-2019 03:09:40    
build   18-May-2019 03:09:40    MockHttpServletRequest:
build   18-May-2019 03:09:40          HTTP Method = POST
build   18-May-2019 03:09:40          Request URI = /api/v1/certification/applications
build   18-May-2019 03:09:40           Parameters = {}
build   18-May-2019 03:09:40              Headers = {Content-Type=[application/json;charset=UTF-8]}
build   18-May-2019 03:09:40                 Body = {"applicationVersion":"1.0"}
build   18-May-2019 03:09:40        Session Attrs = {}
build   18-May-2019 03:09:40    
build   18-May-2019 03:09:40    Handler:
build   18-May-2019 03:09:40                 Type = <redacted>
build   18-May-2019 03:09:40               Method = <redacted>
build   18-May-2019 03:09:40    
build   18-May-2019 03:09:40    Async:
build   18-May-2019 03:09:40        Async started = false
build   18-May-2019 03:09:40         Async result = null
build   18-May-2019 03:09:40    
build   18-May-2019 03:09:40    Resolved Exception:
build   18-May-2019 03:09:40                 Type = null
build   18-May-2019 03:09:40    
build   18-May-2019 03:09:40    ModelAndView:
build   18-May-2019 03:09:40            View name = null
build   18-May-2019 03:09:40                 View = null
build   18-May-2019 03:09:40                Model = null
build   18-May-2019 03:09:40    
build   18-May-2019 03:09:40    FlashMap:
build   18-May-2019 03:09:40           Attributes = null
build   18-May-2019 03:09:40    
build   18-May-2019 03:09:40    MockHttpServletResponse:
build   18-May-2019 03:09:40               Status = 200
build   18-May-2019 03:09:40        Error message = null
build   18-May-2019 03:09:40              Headers = {X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
build   18-May-2019 03:09:40         Content type = null
build   18-May-2019 03:09:40                 Body = 
build   18-May-2019 03:09:40        Forwarded URL = null
build   18-May-2019 03:09:40       Redirected URL = null
build   18-May-2019 03:09:40              Cookies = []
Run Code Online (Sandbox Code Playgroud)

我所有的互联网搜索和 Spring Boot 测试文档阅读都没有结果。这个日志记录来自哪里,我们如何关闭它?

我们已经详尽地扫描了我们的代码,并且确信我们不打印/不负责它。

Sam*_*nen 10

这是来自MockMvc.

看起来您可能已经覆盖了默认值并指示 Spring Boot 始终打印调试输出 MockMvc.

您应该能够通过声明@AutoConfigureMockMvc(printOnlyOnFailure = true)或省略printOnlyOnFailure标志来停用它,因为true它是默认值。

您可以通过print属性配置输出模式——例如,@AutoConfigureMockMvc(print = MockMvcPrint.NONE)