From 06e40afe679dc8987a40d28e562e7294246afa1c Mon Sep 17 00:00:00 2001 From: sophiaquach16 Date: Thu, 22 Feb 2018 22:53:01 -0500 Subject: [PATCH 01/19] Added some missing tests for methods in Owner.java class --- .../samples/petclinic/owner/OwnerTest.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java new file mode 100644 index 000000000..d373fe7eb --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java @@ -0,0 +1,66 @@ +package org.springframework.samples.petclinic.owner; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.core.style.ToStringCreator; + +public class OwnerTest { + + private Owner instance; + + @Before + public void setUp() { + this.instance = new Owner(); + } + + + @Test + public void getSetTelephoneTest() { + // Owner instance = new Owner(); + instance.setTelephone("514 371 9999"); + String result = instance.getTelephone(); + assertEquals("514 371 9999", result); + } + + @Test + public void setGetCityTest() { + // Owner instance = new Owner(); + instance.setCity("Montreal"); + String result = instance.getCity(); + assertEquals("Montreal", result); + } + + @Test + public void toStringTest() { + ToStringCreator creator = new ToStringCreator(instance); + String expected = + creator + .append("id", instance.getId()) + .append("new", instance.isNew()) + .append("lastName", instance.getLastName()) + .append("firstName", instance.getFirstName()) + .append("address", instance.getAddress()) + .append("city", instance.getCity()) + .append("telephone", instance.getTelephone()) + .toString(); + String result = instance.toString(); + assertEquals(expected, result); + } + + @Test + public void setPetsInternalgetPetsInternalTest() { + Pet pet = new Pet(); + instance.addPet(pet); + List result = instance.getPets(); + Pet onlyPet = result.iterator().next(); + + assertEquals(1, result.size()); // Make sure there's only one element in the Collection returned + assertEquals(pet, onlyPet); + } + +} \ No newline at end of file From 38121c46457f2f368ba1b2d8e6e6cd7bd6f1cb11 Mon Sep 17 00:00:00 2001 From: Jad Malek Date: Fri, 23 Feb 2018 12:52:10 -0500 Subject: [PATCH 02/19] [TEST] Added tests for all the methods in the Pet.java (they were all mainly getters and setters). Will attempt to add more tests for this class or others. --- src/main/resources/application.properties | 3 + .../samples/petclinic/owner/PetTests.java | 81 +++++++++++++++++++ .../system/ProductionConfigurationTests.java | 2 +- 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/springframework/samples/petclinic/owner/PetTests.java diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c8d5a5c1a..300538118 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -23,3 +23,6 @@ logging.level.org.springframework=INFO # Active Spring profiles spring.profiles.active=production + +# Adjusting local host to 8090 for application launch +server.port = 8090 diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java new file mode 100644 index 000000000..2c7ac15f4 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java @@ -0,0 +1,81 @@ +package org.springframework.samples.petclinic.owner; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import org.springframework.samples.petclinic.visit.Visit; + +import java.util.List; +import java.util.Set; +import java.util.LinkedHashSet; +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"); + } + + @Test + public void testSetAndGetBirthDate() { + //Converting the current time to a local date and ultimately a date to be input into setBirthDate + this.establishCurrentDateForTesting(); + pet.setBirthDate(this.birthDate); + 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 + PetType walrus = new PetType(); + walrus.setId(36); + walrus.setName("Alex Garland"); + pet.setType(walrus); + 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 + Owner amandeepBhandal = new Owner(); + pet.setOwner(amandeepBhandal); + 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 + Set visitsForTesting = new LinkedHashSet<>(); + pet.setVisitsInternal(visitsForTesting); + Set 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 + Visit visitForTesting = new Visit(); + pet.addVisit(visitForTesting); + List resultOfGetVisits = pet.getVisits(); + Visit onlyVisitInCollection = resultOfGetVisits.iterator().next(); + assertEquals(1, resultOfGetVisits.size()); + assertEquals(visitForTesting, onlyVisitInCollection); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/system/ProductionConfigurationTests.java b/src/test/java/org/springframework/samples/petclinic/system/ProductionConfigurationTests.java index 9636e3623..d247d178e 100644 --- a/src/test/java/org/springframework/samples/petclinic/system/ProductionConfigurationTests.java +++ b/src/test/java/org/springframework/samples/petclinic/system/ProductionConfigurationTests.java @@ -14,7 +14,7 @@ public class ProductionConfigurationTests { @Autowired private VetRepository vets; - + //Test error introduced by addition of 10 commits since forking of our repo from original repo @Test public void testFindAll() throws Exception { vets.findAll(); From 6fbaa583cb108f15ccb7f6d09af71fa69ac8d922 Mon Sep 17 00:00:00 2001 From: Jad Malek Date: Fri, 23 Feb 2018 14:26:59 -0500 Subject: [PATCH 03/19] [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. --- .../petclinic/owner/PetValidatorTests.java | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java new file mode 100644 index 000000000..7551b3e62 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java @@ -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()); + } +} + From 88bc05311ab31ba92a6d16457ace3982db4fa5b1 Mon Sep 17 00:00:00 2001 From: SunXP Date: Sat, 24 Feb 2018 04:51:04 -0500 Subject: [PATCH 04/19] adding more unit tests to for the Owner class --- .../samples/petclinic/owner/OwnerTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java index d373fe7eb..d09c5c1a0 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java @@ -2,6 +2,7 @@ package org.springframework.samples.petclinic.owner; import static org.junit.Assert.*; +import java.util.ArrayList; import java.util.List; import org.junit.Before; @@ -63,4 +64,36 @@ public class OwnerTest { assertEquals(pet, onlyPet); } + @Test + public void getPetTest() { + Pet pet = new Pet(); + pet.setName("Pochi"); + instance.addPet(pet); + + assertEquals(pet, instance.getPet("Pochi")); + assertEquals(pet, instance.getPet("Pochi", false)); //tests pet object exists + assertEquals(null, instance.getPet("Pochi", true)); //tests pet object doesn't exist + } + + @Test + public void getPetsTest() { + Pet pet = new Pet(); + List list = new ArrayList<>(); + list.add(pet); + instance.addPet(pet); + + assertEquals(list, instance.getPets()); + + Pet pet2 = new Pet(); + list.add(pet2); + instance.addPet(pet2); + + assertEquals(list, instance.getPets()); + } + + @Test + public void setGetAddress() { + instance.setAddress("123 FakeStreet"); + assertEquals("123 FakeStreet", instance.getAddress()); + } } \ No newline at end of file From 7c36089f6f376e126f7a6feb24b536007cbde1ae Mon Sep 17 00:00:00 2001 From: sophiaquach16 Date: Sat, 24 Feb 2018 11:52:42 -0500 Subject: [PATCH 05/19] Changed test name in OwnerTest.java --- .../org/springframework/samples/petclinic/owner/OwnerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java index d373fe7eb..effc3c83d 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java @@ -53,7 +53,7 @@ public class OwnerTest { } @Test - public void setPetsInternalgetPetsInternalTest() { + public void setPetgetPetsTest() { Pet pet = new Pet(); instance.addPet(pet); List result = instance.getPets(); From 6cb72af44358964bc77e5ead942fac007429c07b Mon Sep 17 00:00:00 2001 From: SunXP Date: Sat, 24 Feb 2018 16:08:36 -0500 Subject: [PATCH 06/19] Pulled name change correction from sophiaDev --- .../org/springframework/samples/petclinic/owner/OwnerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java index d09c5c1a0..ef09a0c73 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java @@ -54,7 +54,7 @@ public class OwnerTest { } @Test - public void setPetsInternalgetPetsInternalTest() { + public void setPetgetPetsTest() { Pet pet = new Pet(); instance.addPet(pet); List result = instance.getPets(); From 36d98e594971dd6118a4258f9b09a38b736eb198 Mon Sep 17 00:00:00 2001 From: Jad Malek Date: Sat, 24 Feb 2018 16:58:23 -0500 Subject: [PATCH 07/19] [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. --- .../samples/petclinic/owner/PetTests.java | 20 ++-- .../petclinic/owner/PetValidatorTests.java | 104 +++++++++--------- 2 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java index 2c7ac15f4..c2388d0b7 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java @@ -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 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 resultOfGetVisits = pet.getVisits(); Visit onlyVisitInCollection = resultOfGetVisits.iterator().next(); assertEquals(1, resultOfGetVisits.size()); - assertEquals(visitForTesting, onlyVisitInCollection); + assertEquals(visitForTesting.getId(), onlyVisitInCollection.getId()); } } \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java index 7551b3e62..220429ed8 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java @@ -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()); } } From b6d3dd1c117abd177a567517068c235b7e95cbba Mon Sep 17 00:00:00 2001 From: Jad Malek Date: Sat, 24 Feb 2018 17:11:11 -0500 Subject: [PATCH 08/19] [FIX] Further adjusted the assertEquals in PetTests to better compare the two parameters (as pointed out by @amawai) --- .../springframework/samples/petclinic/owner/PetTests.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java index c2388d0b7..c4af2f534 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java @@ -47,7 +47,7 @@ public class PetTests { walrus.setName("Alex Garland"); pet.setType(walrus); PetType resultOfGetType = pet.getType(); - assertEquals(walrus, resultOfGetType); + assertEquals(walrus.getName(), resultOfGetType.getName()); } @Test @@ -56,7 +56,7 @@ public class PetTests { Owner amandeepBhandal = new Owner(); pet.setOwner(amandeepBhandal); Owner resultOfGetOwner = pet.getOwner(); - assertEquals(amandeepBhandal, resultOfGetOwner); + assertEquals(amandeepBhandal.getAddress(), resultOfGetOwner.getAddress()); } @Test @@ -65,7 +65,7 @@ public class PetTests { Set visitsForTesting = new LinkedHashSet<>(); pet.setVisitsInternal(visitsForTesting); Set resultOfGetVisitsInternal = pet.getVisitsInternal(); - assertEquals(visitsForTesting, resultOfGetVisitsInternal); + assertEquals(visitsForTesting.size(), resultOfGetVisitsInternal.size()); } @Test From cd8cee435ef9e84d7f62b9e9e65972a3bff4dbe0 Mon Sep 17 00:00:00 2001 From: Amanda Date: Sat, 24 Feb 2018 17:46:13 -0500 Subject: [PATCH 09/19] Updated pom.xml to match current springclinic master so that application can run --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index bdb939f31..a68944300 100644 --- a/pom.xml +++ b/pom.xml @@ -84,7 +84,7 @@ org.webjars - webjars-locator + webjars-locator-core org.webjars From 23b1d2bb5836c7aa8ea6b4bec9b4a31b08ace6f4 Mon Sep 17 00:00:00 2001 From: Amanda Date: Sat, 24 Feb 2018 17:47:17 -0500 Subject: [PATCH 10/19] Added test for Visit.java and VisitRepository.java --- .../petclinic/visit/VisitRepositoryTest.java | 51 +++++++++++++++++++ .../samples/petclinic/visit/VisitTest.java | 31 +++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/test/java/org/springframework/samples/petclinic/visit/VisitRepositoryTest.java create mode 100644 src/test/java/org/springframework/samples/petclinic/visit/VisitTest.java diff --git a/src/test/java/org/springframework/samples/petclinic/visit/VisitRepositoryTest.java b/src/test/java/org/springframework/samples/petclinic/visit/VisitRepositoryTest.java new file mode 100644 index 000000000..5e096df7e --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/visit/VisitRepositoryTest.java @@ -0,0 +1,51 @@ +package org.springframework.samples.petclinic.visit; + +import java.util.Date; +import java.util.List; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.stereotype.Service; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class)) +public class VisitRepositoryTest { + private static final int VISIT_ID = 1; + private static final String DESCRIPTION = "Visiting a test case"; + private static final Date TODAY = new Date(); + private Visit visit; + private List visitList; + + @Autowired + private VisitRepository visitRepository; + + @Before + public void setUp(){ + visit = new Visit(); + visit.setDescription(DESCRIPTION); + visit.setPetId(VISIT_ID); + visit.setDate(TODAY); + } + + @Test + public void shouldFindSavedVisitInVisitRepository() { + visitRepository.save(visit); + visitList = visitRepository.findByPetId(VISIT_ID); + + assertEquals(visitList.size(), 1); + Visit savedVisit = visitList.get(0); + assertEquals((int) savedVisit.getPetId(), VISIT_ID); + assertEquals(savedVisit.getDescription(), DESCRIPTION); + assertEquals(savedVisit.getDate(), TODAY); + } + + +} diff --git a/src/test/java/org/springframework/samples/petclinic/visit/VisitTest.java b/src/test/java/org/springframework/samples/petclinic/visit/VisitTest.java new file mode 100644 index 000000000..97d550d36 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/visit/VisitTest.java @@ -0,0 +1,31 @@ +package org.springframework.samples.petclinic.visit; + +import java.util.Date; +import static org.junit.Assert.*; +import org.junit.Test; + +public class VisitTest { + private Visit visit; + + @Test + public void testGetDate() { + Date today = new Date(); + visit.setDate(today); + assertEquals(visit.getDate(), today); + } + + @Test + public void testGetDescription() { + String description = "The greatest visit of all time"; + visit.setDescription(description); + assertEquals(visit.getDescription(), description); + } + + @Test + public void testGetPetId() { + int petId = 1; + visit.setPetId(petId); + assertEquals((int) visit.getPetId(), petId); + } + +} From d20b9c70023630aa6ce2234d54eb9b1c4c6522b8 Mon Sep 17 00:00:00 2001 From: sophiaquach16 Date: Sat, 24 Feb 2018 19:30:38 -0500 Subject: [PATCH 11/19] Requested changes of OwnerTest.java --- .../samples/petclinic/owner/OwnerTest.java | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java index effc3c83d..b5609fc82 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java @@ -11,56 +11,58 @@ import org.springframework.core.style.ToStringCreator; public class OwnerTest { - private Owner instance; + private Owner ownerInstance; @Before public void setUp() { - this.instance = new Owner(); + this.ownerInstance = new Owner(); } @Test public void getSetTelephoneTest() { // Owner instance = new Owner(); - instance.setTelephone("514 371 9999"); - String result = instance.getTelephone(); + ownerInstance.setTelephone("514 371 9999"); + String result = ownerInstance.getTelephone(); assertEquals("514 371 9999", result); } @Test public void setGetCityTest() { // Owner instance = new Owner(); - instance.setCity("Montreal"); - String result = instance.getCity(); + ownerInstance.setCity("Montreal"); + String result = ownerInstance.getCity(); assertEquals("Montreal", result); } @Test public void toStringTest() { - ToStringCreator creator = new ToStringCreator(instance); + ToStringCreator creator = new ToStringCreator(ownerInstance); String expected = creator - .append("id", instance.getId()) - .append("new", instance.isNew()) - .append("lastName", instance.getLastName()) - .append("firstName", instance.getFirstName()) - .append("address", instance.getAddress()) - .append("city", instance.getCity()) - .append("telephone", instance.getTelephone()) + .append("id", ownerInstance.getId()) + .append("new", ownerInstance.isNew()) + .append("lastName", ownerInstance.getLastName()) + .append("firstName", ownerInstance.getFirstName()) + .append("address", ownerInstance.getAddress()) + .append("city", ownerInstance.getCity()) + .append("telephone", ownerInstance.getTelephone()) .toString(); - String result = instance.toString(); + String result = ownerInstance.toString(); assertEquals(expected, result); } @Test public void setPetgetPetsTest() { Pet pet = new Pet(); - instance.addPet(pet); - List result = instance.getPets(); + pet.setName("Pogo"); + ownerInstance.addPet(pet); + List result = ownerInstance.getPets(); Pet onlyPet = result.iterator().next(); assertEquals(1, result.size()); // Make sure there's only one element in the Collection returned assertEquals(pet, onlyPet); + assertEquals(pet.getName(), onlyPet.getName()); } } \ No newline at end of file From 8b0ddd27a8a450afd911fe6b36e869e9b8612945 Mon Sep 17 00:00:00 2001 From: SunXP Date: Sat, 24 Feb 2018 22:33:52 -0500 Subject: [PATCH 12/19] Changed instance to ownerInstance, separated getPet tests, added assert for size in getPets --- .../samples/petclinic/owner/OwnerTest.java | 67 +++++++++++-------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java index ef09a0c73..a844c62c1 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerTest.java @@ -12,52 +12,52 @@ import org.springframework.core.style.ToStringCreator; public class OwnerTest { - private Owner instance; + private Owner ownerInstance; @Before public void setUp() { - this.instance = new Owner(); + this.ownerInstance = new Owner(); } @Test public void getSetTelephoneTest() { // Owner instance = new Owner(); - instance.setTelephone("514 371 9999"); - String result = instance.getTelephone(); + ownerInstance.setTelephone("514 371 9999"); + String result = ownerInstance.getTelephone(); assertEquals("514 371 9999", result); } @Test public void setGetCityTest() { // Owner instance = new Owner(); - instance.setCity("Montreal"); - String result = instance.getCity(); + ownerInstance.setCity("Montreal"); + String result = ownerInstance.getCity(); assertEquals("Montreal", result); } @Test public void toStringTest() { - ToStringCreator creator = new ToStringCreator(instance); + ToStringCreator creator = new ToStringCreator(ownerInstance); String expected = creator - .append("id", instance.getId()) - .append("new", instance.isNew()) - .append("lastName", instance.getLastName()) - .append("firstName", instance.getFirstName()) - .append("address", instance.getAddress()) - .append("city", instance.getCity()) - .append("telephone", instance.getTelephone()) + .append("id", ownerInstance.getId()) + .append("new", ownerInstance.isNew()) + .append("lastName", ownerInstance.getLastName()) + .append("firstName", ownerInstance.getFirstName()) + .append("address", ownerInstance.getAddress()) + .append("city", ownerInstance.getCity()) + .append("telephone", ownerInstance.getTelephone()) .toString(); - String result = instance.toString(); + String result = ownerInstance.toString(); assertEquals(expected, result); } @Test public void setPetgetPetsTest() { Pet pet = new Pet(); - instance.addPet(pet); - List result = instance.getPets(); + ownerInstance.addPet(pet); + List result = ownerInstance.getPets(); Pet onlyPet = result.iterator().next(); assertEquals(1, result.size()); // Make sure there's only one element in the Collection returned @@ -65,14 +65,23 @@ public class OwnerTest { } @Test - public void getPetTest() { + public void getPetExistsTest() { Pet pet = new Pet(); pet.setName("Pochi"); - instance.addPet(pet); + ownerInstance.addPet(pet); - assertEquals(pet, instance.getPet("Pochi")); - assertEquals(pet, instance.getPet("Pochi", false)); //tests pet object exists - assertEquals(null, instance.getPet("Pochi", true)); //tests pet object doesn't exist + //tests pet object exists + assertEquals(pet, ownerInstance.getPet("Pochi")); + assertEquals(pet, ownerInstance.getPet("Pochi", false)); + } + + @Test + public void getPetDoesntExistsTest() { + Pet pet = new Pet(); + pet.setName("Pochi"); + ownerInstance.addPet(pet); + //tests pet object doesn't exist + assertEquals(null, ownerInstance.getPet("Pochi", true)); } @Test @@ -80,20 +89,22 @@ public class OwnerTest { Pet pet = new Pet(); List list = new ArrayList<>(); list.add(pet); - instance.addPet(pet); + ownerInstance.addPet(pet); - assertEquals(list, instance.getPets()); + assertEquals(list, ownerInstance.getPets()); + assertEquals(1, list.size()); Pet pet2 = new Pet(); list.add(pet2); - instance.addPet(pet2); + ownerInstance.addPet(pet2); - assertEquals(list, instance.getPets()); + assertEquals(list, ownerInstance.getPets()); + assertEquals(2, list.size()); } @Test public void setGetAddress() { - instance.setAddress("123 FakeStreet"); - assertEquals("123 FakeStreet", instance.getAddress()); + ownerInstance.setAddress("123 FakeStreet"); + assertEquals("123 FakeStreet", ownerInstance.getAddress()); } } \ No newline at end of file From 2b1ea2a3d61ced8199a34db644817733b95a681b Mon Sep 17 00:00:00 2001 From: Jad Malek Date: Sat, 24 Feb 2018 23:06:43 -0500 Subject: [PATCH 13/19] [FIX] As per the requested changes, removed the private method for establishing the current date and made it part of the test setup. Also, made the testSetAndGetOwner more comprehensive by checking that each of the attributes particular to the owner were set and retrieved appropriately. --- .../samples/petclinic/owner/PetTests.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java index c4af2f534..18d0c3b18 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java @@ -16,24 +16,22 @@ public class PetTests { private Pet pet; private Date birthDate; - private void establishCurrentDateForTesting() { + @Before + public void testSetUp() { + //Initialization of pet + this.pet = new Pet(); + PetType dog = new PetType(); + dog.setId(9); + dog.setName("Duncan Jones"); + //Initialization of birthDate + //Converting the current time to a local date and ultimately a date to be input into setBirthDate; 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"); - } - @Test public void testSetAndGetBirthDate() { - //Converting the current time to a local date and ultimately a date to be input into setBirthDate - this.establishCurrentDateForTesting(); pet.setBirthDate(this.birthDate); Date resultOfGetDate = pet.getBirthDate(); assertEquals(this.birthDate, resultOfGetDate); @@ -54,9 +52,16 @@ public class PetTests { public void testSetAndGetOwner() { //Creating a new owner type to test the setters and getters for the pet's owner Owner amandeepBhandal = new Owner(); + amandeepBhandal.setAddress("Off-world Colony"); + amandeepBhandal.setCity("Beirut"); + amandeepBhandal.setTelephone("514-333-3333"); + //Attach the newly created owner to the pet pet.setOwner(amandeepBhandal); Owner resultOfGetOwner = pet.getOwner(); - assertEquals(amandeepBhandal.getAddress(), resultOfGetOwner.getAddress()); + assertEquals(resultOfGetOwner.getAddress(), "Off-world Colony"); + assertEquals(resultOfGetOwner.getCity(), "Beirut"); + assertEquals(resultOfGetOwner.getTelephone(), "514-333-3333"); + assertEquals(resultOfGetOwner.getPetsInternal().size(), 0); } @Test From 09a25b2cd0859ed80bb1828c861ac952b6d23f11 Mon Sep 17 00:00:00 2001 From: artemmikhalitsin Date: Sat, 24 Feb 2018 23:35:38 -0500 Subject: [PATCH 14/19] Implemented Pet Controller tests with Mockito --- .../petclinic/owner/PetControllerMockito.java | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/test/java/org/springframework/samples/petclinic/owner/PetControllerMockito.java diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerMockito.java b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerMockito.java new file mode 100644 index 000000000..3a1481cef --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerMockito.java @@ -0,0 +1,98 @@ +package org.springframework.samples.petclinic.owner; + +import static org.junit.Assert.assertEquals; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.*; + +import org.assertj.core.util.Lists; +import org.junit.Before; +import org.junit.Test; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; + +public class PetControllerMockito { + private PetRepository pets; + private OwnerRepository owners; + private PetController controller; + private BindingResult result; + private ModelMap model; + + private Owner owner; + private Pet pet; + + private final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm"; + + @Before + public void setup() { + pets = mock(PetRepository.class); + owners = mock(OwnerRepository.class); + result = mock(BindingResult.class); + model = mock(ModelMap.class); + owner = mock(Owner.class); + pet = mock(Pet.class); + controller = new PetController(pets, owners); + } + + + //Verifies method calls when a pet already exists + @Test + public void testCreationFormError() { + when(pet.getName()).thenReturn("FIDO"); + when(pet.isNew()).thenReturn(true); + when(owner.getPet("FIDO", true)).thenReturn(pet); + when(result.hasErrors()).thenReturn(true); + + String redirect = controller.processCreationForm(owner, pet, result, model); + + //Pet should be added to owner + verify(owner).addPet(pet); + //Should have an error of a pet with duplicate name + verify(result).rejectValue("name", "duplicate", "already exists"); + //Should set the "pet" of the redirect + verify(model).put("pet", pet); + //Should not save the pet + verify(pets, times(0)).save(anyObject()); + //should redirect to create/update form + assertEquals(redirect, VIEWS_PETS_CREATE_OR_UPDATE_FORM); + } + + @Test + public void testCreationFormSuccess() { + when(pet.getName()).thenReturn("FIDO"); + when(pet.isNew()).thenReturn(false); + when(result.hasErrors()).thenReturn(false); + + String redirect = controller.processCreationForm(owner, pet, result, model); + //Should not make an error + verify(result, times(0)).rejectValue(anyString(), anyString(), anyString()); + //Should save the pet to repository + verify(pets).save(pet); + //Should redirect to a pet page + assertEquals(redirect, "redirect:/owners/{ownerId}"); + } + + @Test + public void testUpdateFormError() { + when(result.hasErrors()).thenReturn(true); + + String redirect = controller.processUpdateForm(pet, result, owner, model); + //Pet's owner should be set + verify(pet).setOwner(owner); + //Should set the "pet" of the redirect + verify(model).put("pet", pet); + assertEquals(redirect, VIEWS_PETS_CREATE_OR_UPDATE_FORM); + } + + @Test + public void testUpdateFormSuccess() { + when(result.hasErrors()).thenReturn(false); + + String redirect = controller.processUpdateForm(pet, result, owner, model); + //Pet should be added to owner + verify(owner).addPet(pet); + //Should save the pet to repository + verify(pets).save(pet); + //Should redirect to an owner page + assertEquals(redirect, "redirect:/owners/{ownerId}"); + } +} From 8f516f8e3d7ca53e3abce25eabdda32e478ad4d7 Mon Sep 17 00:00:00 2001 From: Amanda Date: Sat, 24 Feb 2018 23:39:23 -0500 Subject: [PATCH 15/19] Added comments for clarity --- .../petclinic/visit/VisitRepositoryTest.java | 19 +++++++++---------- .../samples/petclinic/visit/VisitTest.java | 15 +++++++++++---- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/test/java/org/springframework/samples/petclinic/visit/VisitRepositoryTest.java b/src/test/java/org/springframework/samples/petclinic/visit/VisitRepositoryTest.java index 5e096df7e..ea8b232a6 100644 --- a/src/test/java/org/springframework/samples/petclinic/visit/VisitRepositoryTest.java +++ b/src/test/java/org/springframework/samples/petclinic/visit/VisitRepositoryTest.java @@ -3,14 +3,13 @@ package org.springframework.samples.petclinic.visit; import java.util.Date; import java.util.List; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Service; import org.springframework.test.context.junit4.SpringRunner; @@ -38,14 +37,14 @@ public class VisitRepositoryTest { @Test public void shouldFindSavedVisitInVisitRepository() { visitRepository.save(visit); + //Get the list of visits associated to VISIT_ID visitList = visitRepository.findByPetId(VISIT_ID); - - assertEquals(visitList.size(), 1); - Visit savedVisit = visitList.get(0); - assertEquals((int) savedVisit.getPetId(), VISIT_ID); - assertEquals(savedVisit.getDescription(), DESCRIPTION); - assertEquals(savedVisit.getDate(), TODAY); - } - + assertThat(visitList.size()).isEqualTo(1); + Visit savedVisit = visitList.get(0); + + assertThat((int) savedVisit.getPetId()).isEqualTo(VISIT_ID); + assertThat(savedVisit.getDescription()).isEqualTo(DESCRIPTION); + assertThat(savedVisit.getDate()).isEqualTo(TODAY); + } } diff --git a/src/test/java/org/springframework/samples/petclinic/visit/VisitTest.java b/src/test/java/org/springframework/samples/petclinic/visit/VisitTest.java index 97d550d36..4f6f3991a 100644 --- a/src/test/java/org/springframework/samples/petclinic/visit/VisitTest.java +++ b/src/test/java/org/springframework/samples/petclinic/visit/VisitTest.java @@ -1,31 +1,38 @@ package org.springframework.samples.petclinic.visit; import java.util.Date; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Before; import org.junit.Test; public class VisitTest { private Visit visit; + @Before + public void setUp() { + visit = new Visit(); + } + @Test public void testGetDate() { Date today = new Date(); visit.setDate(today); - assertEquals(visit.getDate(), today); + assertThat(visit.getDate()).isEqualTo(today); } @Test public void testGetDescription() { String description = "The greatest visit of all time"; visit.setDescription(description); - assertEquals(visit.getDescription(), description); + assertThat(visit.getDescription()).isEqualTo(description); } @Test public void testGetPetId() { int petId = 1; visit.setPetId(petId); - assertEquals((int) visit.getPetId(), petId); + assertThat((int) visit.getPetId()).isEqualTo(petId); } } From 506639c5d8cef6187e1b2eba8a846427c1cc1796 Mon Sep 17 00:00:00 2001 From: Amanda Date: Sat, 24 Feb 2018 23:40:52 -0500 Subject: [PATCH 16/19] Addition to VisitController tests --- .../samples/petclinic/owner/VisitControllerTests.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java index 08d61360e..ff02bce95 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java @@ -16,10 +16,15 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.samples.petclinic.owner.Pet; import org.springframework.samples.petclinic.owner.PetRepository; import org.springframework.samples.petclinic.owner.VisitController; +import org.springframework.samples.petclinic.visit.Visit; import org.springframework.samples.petclinic.visit.VisitRepository; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; +import static org.mockito.Mockito.verify; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyInt; + /** * Test class for {@link VisitController} * @@ -50,6 +55,7 @@ public class VisitControllerTests { mockMvc.perform(get("/owners/*/pets/{petId}/visits/new", TEST_PET_ID)) .andExpect(status().isOk()) .andExpect(view().name("pets/createOrUpdateVisitForm")); + verify(pets).findById(anyInt()); //Ensure that a pet gets loaded per visit } @Test @@ -60,6 +66,8 @@ public class VisitControllerTests { ) .andExpect(status().is3xxRedirection()) .andExpect(view().name("redirect:/owners/{ownerId}")); + verify(pets).findById(anyInt()); //Ensure that a pet gets loaded per visit + verify(visits).save(any(Visit.class)); //Ensure that upon success, the visit gets saved } @Test @@ -70,6 +78,6 @@ public class VisitControllerTests { .andExpect(model().attributeHasErrors("visit")) .andExpect(status().isOk()) .andExpect(view().name("pets/createOrUpdateVisitForm")); + verify(pets).findById(anyInt()); //Ensure that a pet gets loaded per visit } - } From d2637471beb459cf4af39406da120ca6fa87a5c9 Mon Sep 17 00:00:00 2001 From: Aman Bhandal Date: Sun, 25 Feb 2018 13:44:32 -0500 Subject: [PATCH 17/19] [TEST] Added tests for Person, BaseEntity, and NamedEntity --- .../petclinic/model/BaseEntityTest.java | 30 +++++++++++++++++++ .../petclinic/model/NamedEntityTest.java | 26 ++++++++++++++++ .../samples/petclinic/model/PersonTest.java | 29 ++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 src/test/java/org/springframework/samples/petclinic/model/BaseEntityTest.java create mode 100644 src/test/java/org/springframework/samples/petclinic/model/NamedEntityTest.java create mode 100644 src/test/java/org/springframework/samples/petclinic/model/PersonTest.java diff --git a/src/test/java/org/springframework/samples/petclinic/model/BaseEntityTest.java b/src/test/java/org/springframework/samples/petclinic/model/BaseEntityTest.java new file mode 100644 index 000000000..2dcbd8525 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/model/BaseEntityTest.java @@ -0,0 +1,30 @@ +package org.springframework.samples.petclinic.model; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class BaseEntityTest { + + private BaseEntity baseEntityInstance; + + @Before + public void setUp(){ + baseEntityInstance = new BaseEntity(); + } + + // Expects the baseEntity to have Integer of 100 + @Test + public void testGetAndSetId() { + baseEntityInstance.setId(100); + assertEquals((Integer)100, baseEntityInstance.getId()); + } + + // Expects the baseEntity ID to be null + @Test + public void TestIsNew() { + baseEntityInstance.isNew(); + assertNull(baseEntityInstance.getId()); + } +} diff --git a/src/test/java/org/springframework/samples/petclinic/model/NamedEntityTest.java b/src/test/java/org/springframework/samples/petclinic/model/NamedEntityTest.java new file mode 100644 index 000000000..32315a5fb --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/model/NamedEntityTest.java @@ -0,0 +1,26 @@ +package org.springframework.samples.petclinic.model; + +import org.junit.Before; +import org.junit.Test; + +import javax.naming.Name; + +import static org.junit.Assert.*; + +public class NamedEntityTest { + + private NamedEntity namedObj; + + @Before + public void setUp(){ + namedObj = new NamedEntity(); + } + + // Testing the DAO of NamedEntity class by verifying the setter, getter and toString of Name + @Test + public void testSetAndGetName() { + namedObj.setName("Junior The Senior"); + assertEquals("Junior The Senior", namedObj.getName()); + assertEquals("Junior The Senior", namedObj.toString()); + } +} diff --git a/src/test/java/org/springframework/samples/petclinic/model/PersonTest.java b/src/test/java/org/springframework/samples/petclinic/model/PersonTest.java new file mode 100644 index 000000000..af8f7ac2b --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/model/PersonTest.java @@ -0,0 +1,29 @@ +package org.springframework.samples.petclinic.model; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class PersonTest { + + private Person personObj; + + @Before + public void setUp(){ + personObj = new Person(); + } + + // Testing the DAO of Person class by verifying the setter and getter of FirstName + @Test + public void testSetAndGetFirstName() { + personObj.setFirstName("Johnny"); + } + + // Testing the DAO of Person class by verifying the setter and getter of FirstName + @Test + public void testSetAndGetLastName() { + personObj.setLastName("Oliver"); + assertEquals("Oliver", personObj.getLastName()); + } +} From aa595b12ba34f82d9fc94ebd12360b8eff7b9743 Mon Sep 17 00:00:00 2001 From: Aman Bhandal Date: Sun, 25 Feb 2018 14:26:58 -0500 Subject: [PATCH 18/19] [TEST] Added missing assertEqual for getFirstName --- .../org/springframework/samples/petclinic/model/PersonTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/springframework/samples/petclinic/model/PersonTest.java b/src/test/java/org/springframework/samples/petclinic/model/PersonTest.java index af8f7ac2b..8f98ea77f 100644 --- a/src/test/java/org/springframework/samples/petclinic/model/PersonTest.java +++ b/src/test/java/org/springframework/samples/petclinic/model/PersonTest.java @@ -18,6 +18,7 @@ public class PersonTest { @Test public void testSetAndGetFirstName() { personObj.setFirstName("Johnny"); + assertEquals("Johnny", personObj.getFirstName()); } // Testing the DAO of Person class by verifying the setter and getter of FirstName From 7a44f46c8460ad300e896baba3b59cbebaca5af1 Mon Sep 17 00:00:00 2001 From: Maxim Nguyen Date: Sun, 25 Feb 2018 15:55:38 -0500 Subject: [PATCH 19/19] Added tests for Vet class --- .../samples/petclinic/vet/VetTests.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java b/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java index de3a7b9bb..e3a1f6d59 100644 --- a/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java +++ b/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java @@ -17,9 +17,14 @@ package org.springframework.samples.petclinic.vet; import org.junit.Test; +import org.springframework.beans.support.MutableSortDefinition; +import org.springframework.beans.support.PropertyComparator; import org.springframework.util.SerializationUtils; +import java.util.List; + import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertTrue; /** * @author Dave Syer @@ -40,4 +45,37 @@ public class VetTests { assertThat(other.getId()).isEqualTo(vet.getId()); } + @Test + public void testGetSpecialties(){ + Vet vet = new Vet(); + vet.setFirstName("Zaphod"); + vet.setLastName("Beeblebrox"); + vet.setId(123); + Specialty radiology = new Specialty(); + Specialty urinalysis = new Specialty(); + Specialty kinesiology = new Specialty(); + vet.addSpecialty(radiology); + vet.addSpecialty(urinalysis); + //vet.addSpecialty(kinesiology); + + List testList = vet.getSpecialties(); + assertThat(testList).isEqualTo(vet.getSpecialties()); + testList.getClass().getSimpleName().equals("UnmodifiableCollection"); + } + + @Test + public void testAddGetNrSpecialty(){ + Vet vet = new Vet(); + vet.setFirstName("Zaphod"); + vet.setLastName("Beeblebrox"); + vet.setId(123); + + int preAddedNumber = vet.getNrOfSpecialties(); + Specialty radiology = new Specialty(); + //radiology.setName("radiology"); + vet.addSpecialty(radiology); + assertThat(vet.getNrOfSpecialties()).isEqualTo(++preAddedNumber); + assertThat((vet.getSpecialtiesInternal()).contains(radiology)); + } + }