UnrecognizedPropertyExceptionHandler implements BaseExceptionHandler<UnrecognizedPropertyException> { @ResponseStatus(value = HttpStatus.UNPROCESSABLE_ENTITY) @ExceptionHandler(value = { UnrecognizedPropertyException.class }) @ResponseBody @Override public MessageResource handle(final UnrecognizedPropertyException e) throws Exception { LOG.error("Handling unrecognized exception", e); List<String> fields = e.getKnownPropertyIds().stream().map(property -> property.toString()).collect(Collectors.toList()); return new UnrecognizedPropertyMessageResource(MessageType.ERROR, UNRECOGNIZED_PROPERTY_SENT, e.getPropertyName(), fields); }  @ResponseStatus(value = HttpStatus.UNPROCESSABLE_ENTITY) @ExceptionHandler(value = { UnrecognizedPropertyException.class }) @ResponseBody @Override MessageResource handle(final UnrecognizedPropertyException e); static final String UNRECOGNIZED_PROPERTY_SENT; }
@Test public void testHandle() throws Exception { JsonLocation loc = new JsonLocation(null, 1L, 1, 1, 1); Class<?> clz = UnrecognizedPropertyException.class; Collection<Object> fields = Arrays.asList("field1", "field2"); String propName = "propName"; String message = "message"; UnrecognizedPropertyException e = new UnrecognizedPropertyException(message, loc, clz, propName, fields); MessageResource expected = new UnrecognizedPropertyMessageResource(MessageType.ERROR, "Unrecognized Property sent", propName, fields.stream().map(field -> field.toString()).collect(Collectors.toList())); MessageResource actual = unit.handle(e); assertEquals(expected, actual); }