SearchTourFormDateValidator implements Validator { @Override public void validate(Object target, Errors errors) { SearchTourForm searchTourForm = (SearchTourForm) target; try { new DateTime(searchTourForm.getDepYear(), searchTourForm .getDepMonth(), searchTourForm.getDepDay(), 0, 0).toDate(); } catch (IllegalFieldValueException e) { errors.rejectValue("depYear", "IncorrectDate.inputdate", "Incorrect date was entered."); } }  @Override boolean supports(Class<?> clazz); @Override void validate(Object target, Errors errors);  }
@Test public void testValidate01() { SearchTourFormDateValidator validator = new SearchTourFormDateValidator(); SearchTourForm searchTourForm = new SearchTourForm(); BindingResult result = new DirectFieldBindingResult(searchTourForm, "SearchTourcriteria"); searchTourForm.setDepDay(1); searchTourForm.setDepMonth(1); searchTourForm.setDepYear(2000); validator.validate(searchTourForm, result); assertThat(result.hasErrors(), is(false)); }
@Test public void testValidate02() { SearchTourFormDateValidator validator = new SearchTourFormDateValidator(); SearchTourForm criteria = new SearchTourForm(); BindingResult result = new DirectFieldBindingResult(criteria, "SearchTourcriteria"); criteria.setDepDay(31); criteria.setDepMonth(2); criteria.setDepYear(2000); validator.validate(criteria, result); assertThat(result.hasErrors(), is(true)); FieldError error = result.getFieldError("depYear"); if (error != null) { assertThat(error.getCode(), is("IncorrectDate.inputdate")); assertThat(error.getDefaultMessage(), is( "Incorrect date was entered.")); } else { fail("error"); } }