WebActionControllerAdvice implements ResponseBodyAdvice<Object> { @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Throwable.class) @ResponseBody public MultiExecuteOutput exception(Throwable pEx){ ExecuteOutput<?> resp = new ExecuteOutput<>(); ExecuteError execError = constructExecError(pEx); String message = constructMessage(execError); if (Optional.ofNullable(exceptions).isPresent()) { if (exceptions.containsKey(pEx.getClass())) { message = constructMessage(exceptions.get(pEx.getClass()), execError); } else { Optional<Class<?>> hierarchyclass = exceptions.keySet() .stream() .filter(c -> null != c && c.isAssignableFrom(pEx.getClass())) .findFirst(); if (hierarchyclass.isPresent()) message = constructMessage(exceptions.get(hierarchyclass.get()), execError); } } execError.setMessage(message); logError(execError, pEx); resp.setExecuteException(execError); return defaultInterceptor.handleResponse(resp); }  @Override boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType); @SuppressWarnings("unchecked") @Override Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, 
			Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response); @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Throwable.class) @ResponseBody MultiExecuteOutput exception(Throwable pEx); @ResponseStatus(value = HttpStatus.UNPROCESSABLE_ENTITY) @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseBody MultiExecuteOutput exception(MethodArgumentNotValidException vEx);  }
@Test public void testFrameworkRuntimeExceptionHandler() { final FrameworkRuntimeException ex = new FrameworkRuntimeException(); final MultiExecuteOutput expected = new MultiExecuteOutput(sessionid); Mockito.when(this.interceptor.handleResponse(Mockito.isA(ExecuteOutput.class))).thenReturn(expected); final MultiExecuteOutput actual = this.testee.exception(ex); final ArgumentCaptor<ExecuteOutput> respCaptor = ArgumentCaptor.forClass(ExecuteOutput.class); Mockito.verify(this.interceptor, Mockito.only()).handleResponse(respCaptor.capture()); Assert.assertEquals(expected, actual); Assert.assertEquals(ex.getExecuteError(), respCaptor.getValue().getExecuteException()); }
@Test public void testMethodArgumentNotValidExceptionHandler() throws NoSuchMethodException, SecurityException { final MethodArgumentNotValidException ex = Mockito.mock(MethodArgumentNotValidException.class); final MultiExecuteOutput expected = new MultiExecuteOutput(sessionid); Mockito.when(this.interceptor.handleResponse(Mockito.isA(ExecuteOutput.class))).thenReturn(expected); final MultiExecuteOutput actual = this.testee.exception(ex); final ArgumentCaptor<ExecuteOutput> respCaptor = ArgumentCaptor.forClass(ExecuteOutput.class); Mockito.verify(this.interceptor, Mockito.only()).handleResponse(respCaptor.capture()); Assert.assertEquals(expected, actual); Assert.assertNotNull(respCaptor.getValue().getValidationResult()); Assert.assertNotNull(respCaptor.getValue().getValidationResult().getErrors()); }
@Test @Ignore public void testMethodArgumentNotValidExceptionHandlerNoBindingResult() throws NoSuchMethodException, SecurityException { final MethodParameter methodParameter = Mockito.mock(MethodParameter.class); final MethodArgumentNotValidException ex = new MethodArgumentNotValidException(methodParameter, null); final MultiExecuteOutput expected = new MultiExecuteOutput(sessionid); Mockito.when(methodParameter.getMethod()).thenReturn(this.getClass().getMethods()[0]); Mockito.when(this.interceptor.handleResponse(Mockito.isA(ExecuteOutput.class))).thenReturn(expected); final MultiExecuteOutput actual = this.testee.exception(ex); final ArgumentCaptor<ExecuteOutput> respCaptor = ArgumentCaptor.forClass(ExecuteOutput.class); Mockito.verify(this.interceptor, Mockito.only()).handleResponse(respCaptor.capture()); Assert.assertEquals(expected, actual); Assert.assertNotNull(respCaptor.getValue().getValidationResult()); Assert.assertNotNull(respCaptor.getValue().getValidationResult().getErrors()); }
@Test public void testMethodArgumentNotValidExceptionHandlerWithErrors() throws NoSuchMethodException, SecurityException { final BindingResult bindingResult = Mockito.mock(BindingResult.class); final MethodArgumentNotValidException ex = Mockito.mock(MethodArgumentNotValidException.class); Mockito.when(ex.getBindingResult()).thenReturn(bindingResult); final MultiExecuteOutput expected = new MultiExecuteOutput(sessionid); List<ObjectError> errors = new ArrayList<>(); errors.add(new ObjectError("dashboard", new String[] {"1"}, null, "dashboard-error")); errors.add(new ObjectError("home", new String[] {"2"}, null, "home-error")); Mockito.when(bindingResult.getAllErrors()).thenReturn(errors); Mockito.when(this.interceptor.handleResponse(Mockito.isA(ExecuteOutput.class))).thenReturn(expected); final MultiExecuteOutput actual = this.testee.exception(ex); final ArgumentCaptor<ExecuteOutput> respCaptor = ArgumentCaptor.forClass(ExecuteOutput.class); Mockito.verify(bindingResult, Mockito.times(2)).getAllErrors(); Mockito.verify(this.interceptor, Mockito.only()).handleResponse(respCaptor.capture()); Assert.assertEquals(expected, actual); Assert.assertNotNull(respCaptor.getValue().getValidationResult()); Assert.assertNotNull(respCaptor.getValue().getValidationResult().getErrors()); Assert.assertEquals("dashboard", respCaptor.getValue().getValidationResult().getErrors().get(0).getModelAlias()); Assert.assertEquals("dashboard-error", respCaptor.getValue().getValidationResult().getErrors().get(0).getMsg()); Assert.assertEquals("1", respCaptor.getValue().getValidationResult().getErrors().get(0).getCode()); Assert.assertEquals("home", respCaptor.getValue().getValidationResult().getErrors().get(1).getModelAlias()); Assert.assertEquals("home-error", respCaptor.getValue().getValidationResult().getErrors().get(1).getMsg()); Assert.assertEquals("2", respCaptor.getValue().getValidationResult().getErrors().get(1).getCode()); }