### Question:
PersonNameValidator implements Validator { @Override public void validate(Object object, Errors errors) { if (log.isDebugEnabled()) { log.debug(this.getClass().getName() + ".validate..."); } PersonName personName = (PersonName) object; try { if (personName == null) { errors.reject("error.name"); } else if (!personName.getVoided()) { validatePersonName(personName, errors, false, true); } } catch (Exception e) { errors.reject(e.getMessage()); } }  @Override boolean supports(Class<?> c); @Override void validate(Object object, Errors errors); @Deprecated void validatePersonName(PersonName personName, Errors errors, boolean arrayInd, boolean testInd);  }### Answer:
@Test public void validate_shouldFailValidationIfPersonNameObjectIsNull() { validator.validate(null, errors); assertThat(errors, hasGlobalErrors("error.name")); }
@Test public void validate_shouldPassValidationIfNameIsInvalidButVoided() { personName.setVoided(true); personName.setFamilyName2("34dfgd"); validator.validate(personName, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { personName.setPrefix("prefix"); personName.setGivenName("givenName"); personName.setMiddleName("middleName"); personName.setFamilyNamePrefix("familyNamePrefix"); personName.setFamilyName("familyName"); personName.setFamilyName2("familyName"); personName.setFamilyNameSuffix("familyNameSuffix"); personName.setDegree("degree"); personName.setVoidReason("voidReason"); validator.validate(personName, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() { personName.setPrefix(STRING_OF_51); personName.setGivenName(STRING_OF_51); personName.setMiddleName(STRING_OF_51); personName.setFamilyName(STRING_OF_51); personName.setFamilyNamePrefix(STRING_OF_51); personName.setFamilyName(STRING_OF_51); personName.setFamilyName2(STRING_OF_51); personName.setFamilyNameSuffix(STRING_OF_51); personName.setDegree(STRING_OF_51); personName.setVoidReason(STRING_OF_256); validator.validate(personName, errors); Stream.of("prefix", "givenName", "familyNamePrefix", "familyName", "familyName2", "familyNameSuffix", "degree", "middleName", "voidReason") .forEach(f -> assertThat(errors, hasFieldErrors(f, "error.exceededMaxLengthOfField"))); }
@Test public void validate_shouldReportErrorsOnCorrectFieldNames() { PersonName personName = new PersonName("", "reb", "feb"); MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(), "personName"); validator.validate(personName, errors); assertThat(errors, hasFieldErrors("givenName")); }