Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBootTest and MockMvc creating an enormous amount of console logging - how to disable?

We're using @SpringBootTest with an @Autowired private MockMvc mockMvc attribute to mock HTTP requests to our controller classes.

For some reason that we can't seem to determine, this is creating enormous amounts of logging for each test case, and is filling our logs with thousands of lines of text that looks like:

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 = []

All of my internet searching and Spring Boot Test documentation reading has been fruitless. Where is this logging coming from, and how can we turn it off?

We've scanned our code exhaustively, and are confident that we're not printing/responsible for it.

like image 618
Craig Otis Avatar asked May 18 '19 14:05

Craig Otis


People also ask

Should I enable console logs when running a Spring Boot application?

Usually, console logs give us the opportunity to debug our system in an easy and intuitive way. Nevertheless, there are occasions when we don't want to enable this feature in our system. In this quick tutorial, we'll see how to avoid logging to the console when running a Spring Boot application.

What is mockmvc in Spring Boot?

Spring boot MockMVC example Learn to use Spring MockMVC to perform integration testing of Spring webmvc controllers. MockMVC class is part of Spring MVC test framework which helps in testing the controllers explicitly starting a Servlet container.

How to disable the debug and info logs in Spring Boot?

Run the Spring Boot integration test or unit test, many annoying DEBUG and INFO logs are displayed in the console. To disable the logs, turn off the logging.level in both application.properties and logback-test.xml 1.1 Turn off the logging in application.properties The DEBUG or INFO logs below the Spring banner are off now.

Does @webmvctest auto-configure Spring Security and mockmvc?

By default, tests annotated with @WebMvcTest will also auto-configure Spring Security and MockMvc (include support for HtmlUnit WebClient and Selenium WebDriver). For more fine-grained control of MockMVC the @AutoConfigureMockMvc annotation can be used.


2 Answers

This is coming from MockMvc.

It would appear that you may have overridden the default and instructed Spring Boot to always print the debug output for MockMvc.

You should be able to deactivate this by declaring @AutoConfigureMockMvc(printOnlyOnFailure = true) or by omitting the printOnlyOnFailure flag since true is the default value.

You configure the output mode via the print attribute -- for example, @AutoConfigureMockMvc(print = MockMvcPrint.NONE).

like image 129
Sam Brannen Avatar answered Sep 27 '22 22:09

Sam Brannen


You have to disable info logger from spring framework package in application.properties. Add the below property in application.properties.

logging.level.org.springframework=OFF
like image 28
Sambit Avatar answered Sep 27 '22 21:09

Sambit