Skip to content

Commit c21e823

Browse files
committed
Add validation tests for Person and Owner entities
1 parent edf4db2 commit c21e823

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.junit.jupiter.api.Test;
2525
import org.springframework.context.i18n.LocaleContextHolder;
26+
import org.springframework.samples.petclinic.owner.Owner;
2627
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
2728

2829
import jakarta.validation.ConstraintViolation;
@@ -57,4 +58,60 @@ void shouldNotValidateWhenFirstNameEmpty() {
5758
assertThat(violation.getMessage()).isEqualTo("must not be blank");
5859
}
5960

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+
60117
}

0 commit comments

Comments
 (0)