### Question:
RelationshipValidator implements Validator { @Override public void validate(Object target, Errors errors) { Relationship relationship = (Relationship) target; if (relationship != null) { Date startDate = relationship.getStartDate(); Date endDate = relationship.getEndDate(); if (startDate != null && endDate != null && startDate.after(endDate)) { errors.reject("Relationship.InvalidEndDate.error"); } ValidateUtil.validateFieldLengths(errors, target.getClass(), "voidReason"); if (startDate != null) { Date currentDate = new Date(); if (startDate.after(currentDate)) { errors.reject("error.date.future"); } } } }  @Override boolean supports(Class<?> c); @Override void validate(Object target, Errors errors);  }### Answer:
@Test public void validate_shouldFailIfEndDateIsBeforeStartDate() throws ParseException { Relationship relationship = new Relationship(1); relationship.setStartDate(Context.getDateFormat().parse("18/02/2012")); relationship.setEndDate(Context.getDateFormat().parse("18/02/2001")); Map<String, String> map = new HashMap<>(); MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName()); new RelationshipValidator().validate(relationship, errors); Assert.assertEquals(true, errors.hasErrors()); }
@Test public void validate_shouldPassIfEndDateIsAfterStartDate() throws ParseException { Relationship relationship = new Relationship(1); relationship.setStartDate(Context.getDateFormat().parse("18/02/2012")); relationship.setEndDate(Context.getDateFormat().parse("18/03/2012")); Map<String, String> map = new HashMap<>(); MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName()); new RelationshipValidator().validate(relationship, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { Relationship relationship = new Relationship(1); relationship.setVoidReason("voidReason"); Map<String, String> map = new HashMap<>(); MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName()); new RelationshipValidator().validate(relationship, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() { Relationship relationship = new Relationship(1); relationship .setVoidReason("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text"); Errors errors = new BindException(relationship, "relationship"); new RelationshipValidator().validate(relationship, errors); Assert.assertEquals(true, errors.hasFieldErrors("voidReason")); }
@Test public void validate_shouldFailIfStartDateIsInFuture() { Relationship relationship = new Relationship(1); Map<String, String> map = new HashMap<>(); MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName()); Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, 1); Date nextYear = cal.getTime(); relationship.setStartDate(nextYear); new RelationshipValidator().validate(relationship, errors); Assert.assertTrue(errors.hasErrors()); }
@Test public void validate_shouldPassIfStartDateIsNotInFuture() { Relationship relationship = new Relationship(1); Map<String, String> map = new HashMap<>(); MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName()); Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, -1); Date lastYear = cal.getTime(); relationship.setStartDate(lastYear); new RelationshipValidator().validate(relationship, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldPassIfStartDateIsEmpty() { Map<String, String> map = new HashMap<>(); MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName()); Relationship relationship = new Relationship(1); relationship.setStartDate(null); new RelationshipValidator().validate(relationship, errors); Assert.assertFalse(errors.hasErrors()); }