Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,41 @@ private Validator createValidator() {
return localValidatorFactoryBean;
}

private <T> ConstraintViolation<T> getOnlyViolation(Set<ConstraintViolation<T>> violations) {
assertThat(violations).hasSize(1);
return violations.iterator().next();
}

@Test
void shouldNotValidateWhenFirstNameEmpty() {

LocaleContextHolder.setLocale(Locale.ENGLISH);

Person person = new Person();
person.setFirstName("");
person.setLastName("smith");

Validator validator = createValidator();
Set<ConstraintViolation<Person>> constraintViolations = validator.validate(person);

assertThat(constraintViolations).hasSize(1);
ConstraintViolation<Person> violation = constraintViolations.iterator().next();
ConstraintViolation<Person> violation = getOnlyViolation(constraintViolations);
assertThat(violation.getPropertyPath()).hasToString("firstName");
assertThat(violation.getMessage()).isEqualTo("must not be blank");
}

@Test
void shouldNotValidateWhenLastNameEmpty() {
LocaleContextHolder.setLocale(Locale.ENGLISH);

Person person = new Person();
person.setFirstName("George");
person.setLastName("");

Validator validator = createValidator();
Set<ConstraintViolation<Person>> constraintViolations = validator.validate(person);

ConstraintViolation<Person> violation = getOnlyViolation(constraintViolations);
assertThat(violation.getPropertyPath()).hasToString("lastName");
assertThat(violation.getMessage()).isEqualTo("must not be blank");
}

}