CustomLocalDateDeserializer extends StdDeserializer<LocalDate> { @Override public LocalDate deserialize(JsonParser jsonparser, DeserializationContext context) throws IOException, JsonProcessingException { String date = jsonparser.getText(); if (StringUtils.isEmpty(date) || NULL_STRING.equals(date)) { return null; } for (String DATE_FORMAT : DATE_FORMATS) { try { return LocalDate.parse(date, DateTimeFormatter.ofPattern(DATE_FORMAT)); } catch (Exception e) { } } throw new JsonParseException(jsonparser, "Unparseable date: \"" + date + "\". Supported formats: " + Arrays.toString(DATE_FORMATS)); } CustomLocalDateDeserializer();  CustomLocalDateDeserializer(Class<?> vc); @Override LocalDate deserialize(JsonParser jsonparser, DeserializationContext context);  }
@Test public void t1_deserialize() throws IOException { Mockito.when(this.jsonParser.getText()).thenReturn("1988-10-13T00:00:00.000Z"); final LocalDate result = this.testee.deserialize(this.jsonParser, null); Mockito.verify(this.jsonParser, Mockito.times(1)).getText(); Assert.assertEquals(LocalDate.of(1988, 10, 13), result); }
@Test public void t2_deserializeDifferentFormat() throws IOException { Mockito.when(this.jsonParser.getText()).thenReturn("10/13/1988"); this.thrown.expect(JsonParseException.class); this.thrown.expectMessage("Unparseable date: \"10/13/1988\". Supported formats: [yyyy-MM-dd, yyyy-MM-dd'T'HH:mm:ss.SSS'Z']"); this.testee.deserialize(this.jsonParser, null); }
@Test public void t3_deserializeBadDate() throws IOException { Mockito.when(this.jsonParser.getText()).thenReturn("10-13-1988"); this.thrown.expect(JsonParseException.class); this.thrown.expectMessage("Unparseable date: \"10-13-1988\". Supported formats: [yyyy-MM-dd, yyyy-MM-dd'T'HH:mm:ss.SSS'Z']"); this.testee.deserialize(this.jsonParser, null); }
@Test public void t4_deserialize_nullString() throws IOException { Mockito.when(this.jsonParser.getText()).thenReturn("null"); Assert.assertNull(this.testee.deserialize(this.jsonParser, null)); }