From 38121c46457f2f368ba1b2d8e6e6cd7bd6f1cb11 Mon Sep 17 00:00:00 2001 From: Jad Malek Date: Fri, 23 Feb 2018 12:52:10 -0500 Subject: [PATCH] [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();