title: How to handle @Valid MethodArgumentNotValidException by @RestControllerAdvice
body: <p>Helo,<br>
  I have a problem with testing controller that throws MethodArgumentNotValidException.</p>
  
  <p>I have entity with validation annotation. </p>
  
  <pre><code>public class Tag extends ResourceSupport {
  ...
      @NotEmpty(message = "Tag name cannot be empty.")
      @NotNull(message = "Tag name cannot be empty.")
      private String name;
  }
  </code></pre>
  
  <p>Controller with @Valid annotation for @RequestBody</p>
  
  <pre><code>@RestController
  @RequestMapping(value="/api/tags")
  public class TagController {
      .......
  
      @PostMapping(value = "")
      public ResponseEntity&lt;Tag&gt; addNewTag(@Valid @RequestBody Tag tag) {
          Tag createdTag = tagService.saveOrUpdate(tag);
          return new ResponseEntity&lt;&gt;(createdTag, HttpStatus.CREATED);
      }
  
  }
  </code></pre>
  
  <p>Now I want to test this conttroller with unit test</p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class TagControllerTest {
       private MockMvc mockMvc;
  
       @Before
       public void setup() {
              JacksonTester.initFields(this, new ObjectMapper());
              mockMvc = MockMvcBuilders.standaloneSetup(tagController).setControllerAdvice(new GlobalExceptionHandlerController()).build();
          }
  
      @Test
      public void shouldntAllowToSetTagNameEmpty() throws Exception {
          Tag tag1 = new Tag();
  
          mockMvc.perform(post("/api/tags").contentType(MediaType.APPLICATION_JSON)
              .content(jsonTags.write(tag1).getJson()))
              .andExpect(status().isBadRequest());
  
     }
  </code></pre>
  
  <p>Global exception handling class is created this way.It is only as  example form internet.</p>
  
  <pre><code>@RestControllerAdvice
  public class GlobalExceptionHandlerController extends ResponseEntityExceptionHandler {
  
  @Override
  protected ResponseEntity&lt;Object&gt; handleMethodArgumentNotValid(
          MethodArgumentNotValidException ex,
          HttpHeaders headers,
          HttpStatus status,
          WebRequest request) {
  
      String bodyOfResponse = ex.getMessage();
      return new ResponseEntity(bodyOfResponse, headers, HttpStatus.BAD_REQUEST);
  }
  </code></pre>
  
  <p>Test is passing, as it only test status, but in console I have stack trace with error log</p>
  
  <blockquote>
    <p>org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument at index 0 in method: ..... default message [Tag name cannot be empty.]] </p>
  </blockquote>
  
  <p>Is this correct that error stack trace appear in console log?</p>