### Question:
FieldTypeValidator implements Validator { @Override public void validate(Object obj, Errors errors) { FieldType fieldType = (FieldType) obj; if (fieldType == null) { errors.rejectValue("fieldType", "error.general"); } else { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name"); if (!errors.hasErrors()) { FieldType exist = Context.getFormService().getFieldTypeByName(fieldType.getName()); if (exist != null && !exist.getRetired() && !OpenmrsUtil.nullSafeEquals(fieldType.getUuid(), exist.getUuid())) { errors.rejectValue("name", "fieldtype.duplicate.name"); } } ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name"); } }  @Override boolean supports(Class<?> c); @Override void validate(Object obj, Errors errors);  }### Answer:
@Test public void validate_shouldFailValidationIfNameIsNullOrEmptyOrWhitespace() { FieldType type = new FieldType(); type.setName(null); type.setDescription("Humba humba humba ..."); Errors errors = new BindException(type, "type"); new FieldTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("name")); type.setName(""); errors = new BindException(type, "type"); new FieldTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("name")); type.setName(" "); errors = new BindException(type, "type"); new FieldTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("name")); }
@Test public void validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues() { FieldType type = new FieldType(); type.setName("soccer"); Errors errors = new BindException(type, "type"); new FieldTypeValidator().validate(type, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldFailIfFieldTypeNameIsDuplicate() { FieldType type = new FieldType(); type.setName("some field type"); Errors errors = new BindException(type, "type"); new FieldTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasErrors()); }
@Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { FieldType type = new FieldType(); type.setName("soccer"); Errors errors = new BindException(type, "type"); new FieldTypeValidator().validate(type, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() { FieldType type = new FieldType(); type.setName("too long text too long text too long text too long text"); Errors errors = new BindException(type, "type"); new FieldTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("name")); }