### Question:
RadiologyModalityValidator implements Validator { @Override public void validate(Object obj, Errors errors) { final RadiologyModality radiologyModality = (RadiologyModality) obj; if (radiologyModality == null) { errors.reject("error.general"); } else { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "aeTitle", "error.null", "Cannot be empty or null"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.null", "Cannot be empty or null"); if (radiologyModality.getRetired() && StringUtils.isBlank(radiologyModality.getRetireReason())) { radiologyModality.setRetired(false); errors.rejectValue("retireReason", "error.null"); } ValidateUtil.validateFieldLengths(errors, obj.getClass(), "aeTitle", "name", "description", "retireReason"); } }  @Override boolean supports(Class clazz); @Override void validate(Object obj, Errors errors);  }### Answer:
@Test public void shouldFailValidationIfRadiologyModalityIsNull() throws Exception { radiologyModalityValidator.validate(null, errors); assertSingleGeneralError(errors); }
@Test public void shouldFailValidationIfAeTitleIsNull() throws Exception { radiologyModality.setAeTitle(null); radiologyModalityValidator.validate(radiologyModality, errors); assertSingleNullErrorInField(errors, "aeTitle"); }
@Test public void shouldFailValidationIfAeTitleIsEmpty() throws Exception { radiologyModality.setAeTitle(""); radiologyModalityValidator.validate(radiologyModality, errors); assertSingleNullErrorInField(errors, "aeTitle"); }
@Test public void shouldFailValidationIfAeTitleIsWhitespacesOnly() throws Exception { radiologyModality.setAeTitle(" "); radiologyModalityValidator.validate(radiologyModality, errors); assertSingleNullErrorInField(errors, "aeTitle"); }
@Test public void shouldFailValidationIfNameIsNull() throws Exception { radiologyModality.setName(null); radiologyModalityValidator.validate(radiologyModality, errors); assertSingleNullErrorInField(errors, "name"); }
@Test public void shouldFailValidationIfNameIsEmpty() throws Exception { radiologyModality.setName(""); radiologyModalityValidator.validate(radiologyModality, errors); assertSingleNullErrorInField(errors, "name"); }
@Test public void shouldFailValidationIfNameIsWhitespacesOnly() throws Exception { radiologyModality.setName(" "); radiologyModalityValidator.validate(radiologyModality, errors); assertSingleNullErrorInField(errors, "name"); }
@Test public void shouldFailValidationIfRetireReasonIsNullOrEmptyIfRetireIsTrueAndSetRetiredToFalse() throws Exception { radiologyModality.setRetired(true); radiologyModalityValidator.validate(radiologyModality, errors); assertSingleNullErrorInField(errors, "retireReason"); }
@Test public void shouldFailValidationIfFieldLengthsAreNotCorrect() throws Exception { radiologyModality.setAeTitle(StringUtils.repeat("1", 17)); radiologyModality.setName(StringUtils.repeat("1", 256)); radiologyModality.setDescription(StringUtils.repeat("1", 256)); radiologyModality.setRetireReason(StringUtils.repeat("1", 256)); radiologyModalityValidator.validate(radiologyModality, errors); assertTrue(errors.hasErrors()); ObjectError err = (errors.getAllErrors()).get(0); for (ObjectError error : errors.getAllErrors()) { assertThat(error.getCode(), is("error.exceededMaxLengthOfField")); } assertTrue(errors.hasFieldErrors("aeTitle")); assertTrue(errors.hasFieldErrors("name")); assertTrue(errors.hasFieldErrors("description")); assertTrue(errors.hasFieldErrors("retireReason")); }
@Test public void shouldPassValidationIfAllFieldsAreCorrect() throws Exception { radiologyModalityValidator.validate(radiologyModality, errors); assertFalse(errors.hasErrors()); }