LocalDateConverter implements Converter { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { if (isBlank(value)) { return null; } try { return LocalDate.parse(value); } catch (DateTimeParseException e) { throw new ConverterException(new FacesMessage(SEVERITY_ERROR, "Please provide a date using a format like 2014-12-24", null), e); } }  @Override Object getAsObject(FacesContext context, UIComponent component, String value); @Override String getAsString(FacesContext context, UIComponent component, Object value);  }
@Test public void empty_strings_are_converted_to_null_localdates() { Object localDate = new LocalDateConverter().getAsObject(mock(FacesContext.class), mock(UIComponent.class), ""); assertThat(localDate).isNull(); }
@Test public void null_strings_are_converted_to_null_localdates() { Object localDate = new LocalDateConverter().getAsObject(mock(FacesContext.class), mock(UIComponent.class), null); assertThat(localDate).isNull(); }
@Test public void valid_strings_are_converted_to_matching_localdates() { Object localDate = new LocalDateConverter().getAsObject(mock(FacesContext.class), mock(UIComponent.class), "2012-04-05"); assertThat(localDate).isEqualTo(LocalDate.of(2012, APRIL, 5)); }
@Test(expected = ConverterException.class) public void invalid_date_strings_result_in_a_conversion_exception() { new LocalDateConverter().getAsObject(mock(FacesContext.class), mock(UIComponent.class), "adsdas"); }