MessageNotReadableExceptionHandler implements BaseExceptionHandler<Exception> { @ExceptionHandler(value = { HttpMessageNotReadableException.class, UnrecognizedPropertyException.class}) @ResponseStatus(value = HttpStatus.BAD_REQUEST) @ResponseBody @Override public MessageResource handle(final Exception e) throws Exception { LOGGER.error("Handling message not readable exception", e); MessageResource messageResource = new MessageResource(MessageType.ERROR, e.getMessage()); BaseExceptionHandler exceptionHandler = handlers.get(e.getCause() != null ? e.getCause().getClass().getName() : ""); if (exceptionHandler != null) { messageResource = exceptionHandler.handle(e.getCause()); } return messageResource; }  @ExceptionHandler(value = { HttpMessageNotReadableException.class, UnrecognizedPropertyException.class}) @ResponseStatus(value = HttpStatus.BAD_REQUEST) @ResponseBody @Override MessageResource handle(final Exception e);  }
@Test public void test() throws Exception { String msg = "msg"; HttpMessageNotReadableException e = new HttpMessageNotReadableException(msg); MessageResource expected = new MessageResource(MessageType.ERROR, e.getMessage()); when(handlers.get(anyString())).thenReturn(null); MessageResource actual = unit.handle(e); verify(handlers).get(""); assertEquals(expected, actual); }
@Test public void testHandleWithHanglersExceptionHandler() throws Exception { String msg = "msg"; HttpMessageNotReadableException e = new HttpMessageNotReadableException(msg, new UnrecognizedPropertyException(null, null, null, null, null)); MessageResource expected = new MessageResource(MessageType.ERROR, e.getMessage()); when(handlers.get(anyString())).thenReturn(exceptionHandler); when(exceptionHandler.handle(Matchers.<Throwable>any())).thenReturn(expected); MessageResource actual = unit.handle(e); verify(handlers).get(e.getCause().getClass().getName()); verify(exceptionHandler).handle(e.getCause()); assertEquals(expected, actual); }