### Question:
RoleValidator implements Validator { @Override public void validate(Object obj, Errors errors) { Role role = (Role) obj; if (role == null) { errors.rejectValue("role", "error.general"); } else { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "role", "error.role"); if (!role.getRole().equals(role.getRole().trim())) { errors.rejectValue("role", "error.trailingSpaces"); } } if (obj == null) { throw new IllegalArgumentException("validated role object should not be null"); } else { ValidateUtil.validateFieldLengths(errors, obj.getClass(), "role", "description"); } }  @Override boolean supports(Class<?> c); @Override void validate(Object obj, Errors errors);  }### Answer:
@Test public void validate_shouldFailValidationIfRoleIsNullOrEmptyOrWhitespace() { Role role = new Role(); role.setRole(null); role.setDescription("some text"); Errors errors = new BindException(role, "type"); role.setRole(""); errors = new BindException(role, "role"); new RoleValidator().validate(role, errors); Assert.assertTrue(errors.hasFieldErrors("role")); role.setRole(" "); errors = new BindException(role, "role"); new RoleValidator().validate(role, errors); Assert.assertTrue(errors.hasFieldErrors("role")); }
@Test public void validate_shouldPassValidationIfDescriptionIsNullOrEmptyOrWhitespace() { Role role = new Role(); role.setRole("Bowling race car driver"); role.setDescription(null); Errors errors = new BindException(role, "type"); new RoleValidator().validate(role, errors); Assert.assertFalse(errors.hasFieldErrors("description")); role.setDescription(""); errors = new BindException(role, "role"); new RoleValidator().validate(role, errors); Assert.assertFalse(errors.hasFieldErrors("description")); role.setDescription(" "); errors = new BindException(role, "role"); new RoleValidator().validate(role, errors); Assert.assertFalse(errors.hasFieldErrors("description")); }
@Test public void validate_shouldFailValidationIfRoleHasLeadingOrTrailingSpace() { Role role = new Role(); role.setDescription("some text"); role.setRole(" Bowling race car driver"); Errors errors = new BindException(role, "type"); new RoleValidator().validate(role, errors); Assert.assertTrue(errors.hasFieldErrors("role")); Assert.assertEquals("error.trailingSpaces", errors.getFieldError("role").getCode()); role.setRole("Bowling race car driver "); errors = new BindException(role, "role"); new RoleValidator().validate(role, errors); Assert.assertTrue(errors.hasFieldErrors("role")); Assert.assertEquals("error.trailingSpaces", errors.getFieldError("role").getCode()); }
@Test public void validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues() { Role role = new Role(); role.setRole("Bowling race car driver"); role.setDescription("You don't bowl or race fast cars"); Errors errors = new BindException(role, "type"); new RoleValidator().validate(role, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { Role role = new Role(); role.setRole("Bowling race car driver"); role.setDescription("description"); Errors errors = new BindException(role, "type"); new RoleValidator().validate(role, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() { Role role = new Role(); role .setRole("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"); role .setDescription("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(role, "type"); new RoleValidator().validate(role, errors); Assert.assertTrue(errors.hasFieldErrors("role")); }