diff --git a/src/test/java/org/springframework/samples/petclinic/model/PetValidatorTest.java b/src/test/java/org/springframework/samples/petclinic/model/PetValidatorTest.java new file mode 100644 index 000000000..e85c6fbea --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/model/PetValidatorTest.java @@ -0,0 +1,43 @@ +package org.springframework.samples.petclinic.model; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.samples.petclinic.owner.Pet; +import org.springframework.samples.petclinic.owner.PetValidator; +import org.springframework.validation.BeanPropertyBindingResult; +import org.springframework.validation.Errors; +import org.springframework.validation.Validator; + +/** + * @author Oğuz Çağıran + */ +public class PetValidatorTest { + + private Validator validator; + + @Before + public void setup() { + validator = new PetValidator(); + } + + @Test + public void shouldNotValidateWhenPetNameEmpty() { + Pet pet = new Pet(); + pet.setName(""); + Errors errors = new BeanPropertyBindingResult(pet, "name"); + validator.validate(pet, errors); + assertTrue(errors.hasErrors()); + } + + @Test + public void shouldNotValideWhenBirthDateEmpty() { + Pet pet = new Pet(); + pet.setName("pet"); + pet.setBirthDate(null); + Errors errors = new BeanPropertyBindingResult(pet, "birthDate"); + validator.validate(pet, errors); + assertTrue(errors.hasErrors()); + } +}