ServiceBrokerWebMvcExceptionHandler extends ServiceBrokerExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ErrorMessage handleException(MethodArgumentNotValidException ex) { return handleBindingException(ex, ex.getBindingResult()); }  @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) ErrorMessage handleException(MethodArgumentNotValidException ex); @ExceptionHandler(MissingServletRequestParameterException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) ErrorMessage handleException(MissingServletRequestParameterException ex);  }
@Test void methodArgumentNotValidException() throws NoSuchMethodException { BindingResult bindingResult = new MapBindingResult(new HashMap<>(), "objectName"); bindingResult.addError(new FieldError("objectName", "field1", "message")); bindingResult.addError(new FieldError("objectName", "field2", "message")); Method method = this.getClass().getMethod("setUp", (Class<?>[]) null); MethodParameter parameter = new MethodParameter(method, -1); MethodArgumentNotValidException exception = new MethodArgumentNotValidException(parameter, bindingResult); ErrorMessage errorMessage = exceptionHandler.handleException(exception); assertThat(errorMessage.getError()).isNull(); assertThat(errorMessage.getMessage()).contains("field1"); assertThat(errorMessage.getMessage()).contains("field2"); }
@Test void stringParameterIsNotPresent() { final String parameterType = "String"; final String parameterName = "plan_id"; MissingServletRequestParameterException exception = new MissingServletRequestParameterException(parameterName, parameterType); ErrorMessage errorMessage = exceptionHandler.handleException(exception); assertThat(errorMessage.getError()).isNull(); assertThat(errorMessage.getMessage()).contains(parameterType); assertThat(errorMessage.getMessage()).contains(parameterName); }