From 0daa185a82d28b05a52049886d01b14abd407f13 Mon Sep 17 00:00:00 2001 From: Graziela Lucena <128810502+grazixzdev@users.noreply.github.com> Date: Sat, 18 Apr 2026 09:54:30 -0300 Subject: [PATCH] Add missing validation test for Person.lastName Adds a test to validate that Person.lastName must not be blank. This complements the existing firstName validation test. Signed-off-by: Graziela Lucena <128810502+grazixzdev@users.noreply.github.com> --- .../petclinic/model/ValidatorTests.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java b/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java index 559311ff842..76b2b4c8743 100644 --- a/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java +++ b/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java @@ -40,10 +40,15 @@ private Validator createValidator() { return localValidatorFactoryBean; } + private ConstraintViolation getOnlyViolation(Set> 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"); @@ -51,10 +56,25 @@ void shouldNotValidateWhenFirstNameEmpty() { Validator validator = createValidator(); Set> constraintViolations = validator.validate(person); - assertThat(constraintViolations).hasSize(1); - ConstraintViolation violation = constraintViolations.iterator().next(); + ConstraintViolation 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> constraintViolations = validator.validate(person); + + ConstraintViolation violation = getOnlyViolation(constraintViolations); + assertThat(violation.getPropertyPath()).hasToString("lastName"); + assertThat(violation.getMessage()).isEqualTo("must not be blank"); + } + }