CustomerValidator implements Validator { @Override public void validate(@NonNull Object target, Errors errors) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "message.firstName", "FirstName is Mandatory"); final Customer customer = (Customer) target; final LocalDateTime dob = customer.getDateOfBirth(); if (Objects.nonNull(dob) && dob.isAfter(LocalDateTime.now())) { errors.rejectValue("dateOfBirth", ERROR_CODE, "Date Of Birth Should be before today"); errors.reject(ERROR_CODE, "Entity Not Processable"); } }  @Override boolean supports(Class<?> clazz); @Override void validate(@NonNull Object target, Errors errors);  }
@Test void shouldNotValidateWhenFirstNameEmpty() { LocaleContextHolder.setLocale(Locale.ENGLISH); Customer customer = new Customer(); customer.setLastName("kolli"); Validator validator = createValidator(); Set<ConstraintViolation<Customer>> constraintViolations = validator.validate(customer); assertThat(constraintViolations.size()).isEqualTo(1); ConstraintViolation<Customer> violation = constraintViolations.iterator().next(); assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName"); assertThat(violation.getMessage()).isEqualTo("Customer firstName is mandatory"); }