mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:55:49 +00:00
[FIX] Adjusted some of the code formatting and an assertEquals in the PetTests. Also, attempted to mock out the storage dependency purely for testing; however, since isolating the core application's spring-based use of db was more difficult than expected; the code was removed.
This commit is contained in:
parent
6fbaa583cb
commit
36d98e5949
2 changed files with 62 additions and 62 deletions
|
@ -12,22 +12,22 @@ import java.time.*;
|
|||
import java.util.Date;
|
||||
|
||||
public class PetTests {
|
||||
|
||||
|
||||
private Pet pet;
|
||||
private Date birthDate;
|
||||
|
||||
|
||||
private void establishCurrentDateForTesting() {
|
||||
LocalDateTime timePoint = LocalDateTime.now();
|
||||
LocalDate localDate = timePoint.toLocalDate();
|
||||
birthDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
||||
}
|
||||
|
||||
|
||||
@Before
|
||||
public void testSetUp() {
|
||||
this.pet = new Pet();
|
||||
PetType dog = new PetType();
|
||||
dog.setId(9);
|
||||
dog.setName("Duncan Jones");
|
||||
dog.setId(9);
|
||||
dog.setName("Duncan Jones");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -38,7 +38,7 @@ public class PetTests {
|
|||
Date resultOfGetDate = pet.getBirthDate();
|
||||
assertEquals(this.birthDate, resultOfGetDate);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSetAndGetType() {
|
||||
//Creating a new pet type to test the setters and getters for pet's type
|
||||
|
@ -49,7 +49,7 @@ public class PetTests {
|
|||
PetType resultOfGetType = pet.getType();
|
||||
assertEquals(walrus, resultOfGetType);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSetAndGetOwner() {
|
||||
//Creating a new owner type to test the setters and getters for the pet's owner
|
||||
|
@ -58,7 +58,7 @@ public class PetTests {
|
|||
Owner resultOfGetOwner = pet.getOwner();
|
||||
assertEquals(amandeepBhandal, resultOfGetOwner);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSetAndGetVisitsInternal() {
|
||||
//Creating a new set of visits, albeit an empty set, to test the setters and getters for the pet's visits
|
||||
|
@ -67,7 +67,7 @@ public class PetTests {
|
|||
Set<Visit> resultOfGetVisitsInternal = pet.getVisitsInternal();
|
||||
assertEquals(visitsForTesting, resultOfGetVisitsInternal);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testAddVisitAndGetVisits() {
|
||||
//Creating a new set of visits, albeit an empty set, to test the setters and getters for the pet's visits
|
||||
|
@ -76,6 +76,6 @@ public class PetTests {
|
|||
List<Visit> resultOfGetVisits = pet.getVisits();
|
||||
Visit onlyVisitInCollection = resultOfGetVisits.iterator().next();
|
||||
assertEquals(1, resultOfGetVisits.size());
|
||||
assertEquals(visitForTesting, onlyVisitInCollection);
|
||||
assertEquals(visitForTesting.getId(), onlyVisitInCollection.getId());
|
||||
}
|
||||
}
|
|
@ -16,97 +16,97 @@ public class PetValidatorTests {
|
|||
public void testValidationWithValidName() {
|
||||
//given
|
||||
PetValidator petValidator = new PetValidator();
|
||||
Pet validPet = new Pet();
|
||||
validPet.setName("Peter");
|
||||
Errors errors = new BeanPropertyBindingResult(validPet, "");
|
||||
Pet validPet = new Pet();
|
||||
validPet.setName("Peter");
|
||||
Errors errors = new BeanPropertyBindingResult(validPet, "");
|
||||
|
||||
//when
|
||||
petValidator.validate(validPet, errors);
|
||||
//when
|
||||
petValidator.validate(validPet, errors);
|
||||
|
||||
//then
|
||||
assertNull(errors.getFieldError("name"));
|
||||
//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, "");
|
||||
Pet invalidPet = new Pet();
|
||||
invalidPet.setName("");
|
||||
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
|
||||
|
||||
//when
|
||||
petValidator.validate(invalidPet, errors);
|
||||
//when
|
||||
petValidator.validate(invalidPet, errors);
|
||||
|
||||
//then
|
||||
assertTrue(errors.hasErrors());
|
||||
//then
|
||||
assertTrue(errors.hasErrors());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testValidationWithValidType() {
|
||||
//given
|
||||
//given
|
||||
PetValidator petValidator = new PetValidator();
|
||||
Pet validPet = new Pet();
|
||||
PetType tiger = new PetType();
|
||||
Pet validPet = new Pet();
|
||||
PetType tiger = new PetType();
|
||||
tiger.setId(24);
|
||||
validPet.setType(tiger);
|
||||
Errors errors = new BeanPropertyBindingResult(validPet, "");
|
||||
Errors errors = new BeanPropertyBindingResult(validPet, "");
|
||||
|
||||
//when
|
||||
petValidator.validate(validPet, errors);
|
||||
//when
|
||||
petValidator.validate(validPet, errors);
|
||||
|
||||
//then
|
||||
assertNull(errors.getFieldError("type"));
|
||||
//then
|
||||
assertNull(errors.getFieldError("type"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testValidationWithInvalidType() {
|
||||
//given
|
||||
//given
|
||||
PetValidator petValidator = new PetValidator();
|
||||
Pet invalidPet = new Pet();
|
||||
PetType emptyType = null;
|
||||
Pet invalidPet = new Pet();
|
||||
PetType emptyType = null;
|
||||
invalidPet.setType(emptyType);
|
||||
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
|
||||
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
|
||||
|
||||
//when
|
||||
petValidator.validate(invalidPet, errors);
|
||||
//when
|
||||
petValidator.validate(invalidPet, errors);
|
||||
|
||||
//then
|
||||
assertTrue(errors.hasErrors());
|
||||
//then
|
||||
assertTrue(errors.hasErrors());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testValidationWithValidBirthDate() {
|
||||
//given
|
||||
//given
|
||||
PetValidator petValidator = new PetValidator();
|
||||
Pet validPet = new Pet();
|
||||
LocalDateTime timePoint = LocalDateTime.now();
|
||||
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, "");
|
||||
validPet.setBirthDate(birthDate);
|
||||
Errors errors = new BeanPropertyBindingResult(validPet, "");
|
||||
|
||||
//when
|
||||
petValidator.validate(validPet, errors);
|
||||
//when
|
||||
petValidator.validate(validPet, errors);
|
||||
|
||||
//then
|
||||
assertNull(errors.getFieldError("birthDate"));
|
||||
//then
|
||||
assertNull(errors.getFieldError("birthDate"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testValidationWithInvalidBirthDate() {
|
||||
//given
|
||||
//given
|
||||
PetValidator petValidator = new PetValidator();
|
||||
Pet invalidPet = new Pet();
|
||||
Pet invalidPet = new Pet();
|
||||
Date birthDate = null;
|
||||
invalidPet.setBirthDate(birthDate);
|
||||
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
|
||||
invalidPet.setBirthDate(birthDate);
|
||||
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
|
||||
|
||||
//when
|
||||
petValidator.validate(invalidPet, errors);
|
||||
//when
|
||||
petValidator.validate(invalidPet, errors);
|
||||
|
||||
//then
|
||||
assertTrue(errors.hasErrors());
|
||||
//then
|
||||
assertTrue(errors.hasErrors());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue