mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:55:49 +00:00
[TEST] Added tests for the PetValidator.java class. These tests will ensure that the proper validation of name, type and birthDate by testing valid and invalid parameter inputs.
This commit is contained in:
parent
38121c4645
commit
6fbaa583cb
1 changed files with 112 additions and 0 deletions
|
@ -0,0 +1,112 @@
|
|||
package org.springframework.samples.petclinic.owner;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.validation.BeanPropertyBindingResult;
|
||||
import org.springframework.validation.Errors;
|
||||
|
||||
public class PetValidatorTests {
|
||||
|
||||
@Test
|
||||
public void testValidationWithValidName() {
|
||||
//given
|
||||
PetValidator petValidator = new PetValidator();
|
||||
Pet validPet = new Pet();
|
||||
validPet.setName("Peter");
|
||||
Errors errors = new BeanPropertyBindingResult(validPet, "");
|
||||
|
||||
//when
|
||||
petValidator.validate(validPet, errors);
|
||||
|
||||
//then
|
||||
assertNull(errors.getFieldError("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidationWithInvalidName() {
|
||||
//given
|
||||
PetValidator petValidator = new PetValidator();
|
||||
Pet invalidPet = new Pet();
|
||||
invalidPet.setName("");
|
||||
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
|
||||
|
||||
//when
|
||||
petValidator.validate(invalidPet, errors);
|
||||
|
||||
//then
|
||||
assertTrue(errors.hasErrors());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidationWithValidType() {
|
||||
//given
|
||||
PetValidator petValidator = new PetValidator();
|
||||
Pet validPet = new Pet();
|
||||
PetType tiger = new PetType();
|
||||
tiger.setId(24);
|
||||
validPet.setType(tiger);
|
||||
Errors errors = new BeanPropertyBindingResult(validPet, "");
|
||||
|
||||
//when
|
||||
petValidator.validate(validPet, errors);
|
||||
|
||||
//then
|
||||
assertNull(errors.getFieldError("type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidationWithInvalidType() {
|
||||
//given
|
||||
PetValidator petValidator = new PetValidator();
|
||||
Pet invalidPet = new Pet();
|
||||
PetType emptyType = null;
|
||||
invalidPet.setType(emptyType);
|
||||
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
|
||||
|
||||
//when
|
||||
petValidator.validate(invalidPet, errors);
|
||||
|
||||
//then
|
||||
assertTrue(errors.hasErrors());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidationWithValidBirthDate() {
|
||||
//given
|
||||
PetValidator petValidator = new PetValidator();
|
||||
Pet validPet = new Pet();
|
||||
LocalDateTime timePoint = LocalDateTime.now();
|
||||
LocalDate localDate = timePoint.toLocalDate();
|
||||
Date birthDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
||||
validPet.setBirthDate(birthDate);
|
||||
Errors errors = new BeanPropertyBindingResult(validPet, "");
|
||||
|
||||
//when
|
||||
petValidator.validate(validPet, errors);
|
||||
|
||||
//then
|
||||
assertNull(errors.getFieldError("birthDate"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidationWithInvalidBirthDate() {
|
||||
//given
|
||||
PetValidator petValidator = new PetValidator();
|
||||
Pet invalidPet = new Pet();
|
||||
Date birthDate = null;
|
||||
invalidPet.setBirthDate(birthDate);
|
||||
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
|
||||
|
||||
//when
|
||||
petValidator.validate(invalidPet, errors);
|
||||
|
||||
//then
|
||||
assertTrue(errors.hasErrors());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in a new issue