### Question:
ParseDate implements IsParser<Date> { @Override public Date parse(JsonParser parser, Field field) throws IOException { if (!isValid(parser)) { return null; } JsonFormat formatAnn = field.getAnnotation(JsonFormat.class); if (formatAnn == null) { throw new RuntimeException("JsonFormat with pattern needed for: " + field.getName()); } try { return new SimpleDateFormat(formatAnn.pattern(), Locale.getDefault()).parse(parser.getValueAsString()); } catch (ParseException e) { throw new RuntimeException("JsonFormat with pattern is wrong for: " + field.getName() + " pattern: " + formatAnn.pattern()); } }  @Override Date parse(JsonParser parser, Field field);  }### Answer:
@Test public void parse() throws Exception { Date expected = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse("2016-10-31 09:10:12.2"); Field field = getClass().getField("testDate"); ParseDate parseDate = new ParseDate(); Date actual = parseDate.parse(ParserUtil.getParser("<date>2016-10-31 09:10:12.2</date>"), field); assertEquals(expected, actual); }
@Test public void parseEmpty() throws Exception { Date expected = null; Field field = getClass().getField("testDate"); ParseDate parseDate = new ParseDate(); Date actual = parseDate.parse(ParserUtil.getParser("<date></date>"), field); assertEquals(expected, actual); }
@Test public void parseSpace() throws Exception { Date expected = null; Field field = getClass().getField("testDate"); ParseDate parseDate = new ParseDate(); Date actual = parseDate.parse(ParserUtil.getParser("<date> </date>"), field); assertEquals(expected, actual); }
@Test(expected = RuntimeException.class) public void parseNoFormat() throws Exception { Field field = getClass().getField("testDateNoPattern"); ParseDate parseDate = new ParseDate(); parseDate.parse(ParserUtil.getParser("<date>2016-10-31 09:10:12.2</date>"), field); }
@Test(expected = RuntimeException.class) public void parseWrongFormat() throws Exception { Field field = getClass().getField("testDate"); ParseDate parseDate = new ParseDate(); parseDate.parse(ParserUtil.getParser("<date>2016-10-3109:10:12</date>"), field); }