### Question:
PersonMergeLogValidator implements Validator { @Override public void validate(Object obj, Errors errors) { PersonMergeLog personMergeLog = (PersonMergeLog) obj; if (personMergeLog == null) { errors.rejectValue("persnMergeLog", "error.general"); } else { ValidationUtils.rejectIfEmpty(errors, "personMergeLogData", "error.null"); ValidationUtils.rejectIfEmpty(errors, "winner", "error.null"); ValidationUtils.rejectIfEmpty(errors, "loser", "error.null"); ValidateUtil.validateFieldLengths(errors, obj.getClass(), "voidReason"); } }  @Override boolean supports(Class<?> c); @Override void validate(Object obj, Errors errors);  }### Answer:
@Test public void validate_shouldFailValidationIfPersonMergeLogDataIsNull() { PersonMergeLog personMergeLog = new PersonMergeLog(); personMergeLog.setWinner(new Person()); personMergeLog.setLoser(new Person()); PersonMergeLogValidator validator = new PersonMergeLogValidator(); Errors errors = new BindException(personMergeLog, "personMergeLog"); validator.validate(personMergeLog, errors); Assert.assertTrue(errors.hasFieldErrors("personMergeLogData")); }
@Test public void validate_shouldFailValidationIfWinnerIsNull() { PersonMergeLog personMergeLog = new PersonMergeLog(); personMergeLog.setLoser(new Person()); personMergeLog.setPersonMergeLogData(new PersonMergeLogData()); PersonMergeLogValidator validator = new PersonMergeLogValidator(); Errors errors = new BindException(personMergeLog, "personMergeLog"); validator.validate(personMergeLog, errors); Assert.assertTrue(errors.hasFieldErrors("winner")); }
@Test public void validate_shouldFailValidationIfLoserIsNull() { PersonMergeLog personMergeLog = new PersonMergeLog(); personMergeLog.setWinner(new Person()); personMergeLog.setPersonMergeLogData(new PersonMergeLogData()); PersonMergeLogValidator validator = new PersonMergeLogValidator(); Errors errors = new BindException(personMergeLog, "personMergeLog"); validator.validate(personMergeLog, errors); Assert.assertTrue(errors.hasFieldErrors("loser")); }
@Test public void validate_shouldPassValidationIfAllFieldsAreCorrect() { PersonMergeLog personMergeLog = new PersonMergeLog(); personMergeLog.setWinner(new Person()); personMergeLog.setLoser(new Person()); personMergeLog.setPersonMergeLogData(new PersonMergeLogData()); PersonMergeLogValidator validator = new PersonMergeLogValidator(); Errors errors = new BindException(personMergeLog, "personMergeLog"); validator.validate(personMergeLog, errors); Assert.assertFalse(errors.hasFieldErrors()); }
@Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { PersonMergeLog personMergeLog = new PersonMergeLog(); personMergeLog.setWinner(new Person()); personMergeLog.setLoser(new Person()); personMergeLog.setPersonMergeLogData(new PersonMergeLogData()); personMergeLog.setVoidReason("voidReason"); PersonMergeLogValidator validator = new PersonMergeLogValidator(); Errors errors = new BindException(personMergeLog, "personMergeLog"); validator.validate(personMergeLog, errors); Assert.assertFalse(errors.hasFieldErrors()); }
@Test public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() { PersonMergeLog personMergeLog = new PersonMergeLog(); personMergeLog.setWinner(new Person()); personMergeLog.setLoser(new Person()); personMergeLog.setPersonMergeLogData(new PersonMergeLogData()); personMergeLog .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"); PersonMergeLogValidator validator = new PersonMergeLogValidator(); Errors errors = new BindException(personMergeLog, "personMergeLog"); validator.validate(personMergeLog, errors); Assert.assertTrue(errors.hasFieldErrors("voidReason")); }