|
23 | 23 |
|
24 | 24 | import org.junit.jupiter.api.Test; |
25 | 25 | import org.springframework.context.i18n.LocaleContextHolder; |
| 26 | +import org.springframework.samples.petclinic.owner.Owner; |
26 | 27 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; |
27 | 28 |
|
28 | 29 | import jakarta.validation.ConstraintViolation; |
@@ -57,4 +58,60 @@ void shouldNotValidateWhenFirstNameEmpty() { |
57 | 58 | assertThat(violation.getMessage()).isEqualTo("must not be blank"); |
58 | 59 | } |
59 | 60 |
|
| 61 | + @Test |
| 62 | + void shouldNotValidateWhenLastNameEmpty() { |
| 63 | + |
| 64 | + LocaleContextHolder.setLocale(Locale.ENGLISH); |
| 65 | + Person person = new Person(); |
| 66 | + person.setFirstName("James"); |
| 67 | + person.setLastName(""); |
| 68 | + |
| 69 | + Validator validator = createValidator(); |
| 70 | + Set<ConstraintViolation<Person>> constraintViolations = validator.validate(person); |
| 71 | + |
| 72 | + assertThat(constraintViolations).hasSize(1); |
| 73 | + ConstraintViolation<Person> violation = constraintViolations.iterator().next(); |
| 74 | + assertThat(violation.getPropertyPath()).hasToString("lastName"); |
| 75 | + assertThat(violation.getMessage()).isEqualTo("must not be blank"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void shouldNotValidateOwnerWithBlankFieldsAndNonNumericTelephone() { |
| 80 | + |
| 81 | + LocaleContextHolder.setLocale(Locale.ENGLISH); |
| 82 | + Owner owner = new Owner(); |
| 83 | + owner.setFirstName(""); |
| 84 | + owner.setLastName(""); |
| 85 | + owner.setAddress(""); |
| 86 | + owner.setCity(""); |
| 87 | + owner.setTelephone("telephone"); |
| 88 | + |
| 89 | + Validator validator = createValidator(); |
| 90 | + Set<ConstraintViolation<Owner>> constraintViolations = validator.validate(owner); |
| 91 | + |
| 92 | + assertThat(constraintViolations).hasSize(5); |
| 93 | + assertThat(constraintViolations).extracting(violation -> violation.getPropertyPath().toString()) |
| 94 | + .containsExactlyInAnyOrder("firstName", "lastName", "address", "city", "telephone"); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void shouldNotValidateOwnerWhenTelephoneIsNonNumeric() { |
| 99 | + |
| 100 | + LocaleContextHolder.setLocale(Locale.ENGLISH); |
| 101 | + Owner owner = new Owner(); |
| 102 | + owner.setFirstName("George"); |
| 103 | + owner.setLastName("Franklin"); |
| 104 | + owner.setAddress("110 W. Liberty St."); |
| 105 | + owner.setCity("Madison"); |
| 106 | + owner.setTelephone("invalid"); |
| 107 | + |
| 108 | + Validator validator = createValidator(); |
| 109 | + Set<ConstraintViolation<Owner>> constraintViolations = validator.validate(owner); |
| 110 | + |
| 111 | + assertThat(constraintViolations).hasSize(1); |
| 112 | + ConstraintViolation<Owner> violation = constraintViolations.iterator().next(); |
| 113 | + assertThat(violation.getPropertyPath()).hasToString("telephone"); |
| 114 | + assertThat(violation.getMessage()).isEqualTo("numeric value out of bounds (<10 digits>.<0 digits> expected)"); |
| 115 | + } |
| 116 | + |
60 | 117 | } |
0 commit comments