From a6f47e8bfa52d1582d9a8d72d190ab9aeb0d58de Mon Sep 17 00:00:00 2001 From: Dee Date: Thu, 8 Jul 2021 22:22:17 -0400 Subject: [PATCH] TDD tests created and passed --- .../java/rocks/zipcodewilmington/Food.java | 12 ++ .../rocks/zipcodewilmington/animals/Cat.java | 5 + .../zipcodewilmington/animals/Mammal.java | 10 +- .../zipcodewilmington/AnimalFactoryTest.java | 43 ++++++ .../rocks/zipcodewilmington/CatHouseTest.java | 83 +++++++++++ .../java/rocks/zipcodewilmington/CatTest.java | 139 ++++++++++++++++++ .../rocks/zipcodewilmington/DogHouseTest.java | 81 ++++++++++ .../java/rocks/zipcodewilmington/DogTest.java | 108 ++++++++++++++ 8 files changed, 480 insertions(+), 1 deletion(-) diff --git a/src/main/java/rocks/zipcodewilmington/Food.java b/src/main/java/rocks/zipcodewilmington/Food.java index 2e5056b..8d5fbb2 100644 --- a/src/main/java/rocks/zipcodewilmington/Food.java +++ b/src/main/java/rocks/zipcodewilmington/Food.java @@ -4,4 +4,16 @@ * @author leon on 4/19/18. */ public class Food { + private String brand; + private int ounces; + private String typeOfMeat; + private boolean isWet; + + public Food(String brand, int ounces, String typeOfMeat, boolean isWet){ + this.brand = brand; + this.ounces = ounces; + this.typeOfMeat = typeOfMeat; + this.isWet = isWet; + + } } diff --git a/src/main/java/rocks/zipcodewilmington/animals/Cat.java b/src/main/java/rocks/zipcodewilmington/animals/Cat.java index e703c36..85cc5c5 100644 --- a/src/main/java/rocks/zipcodewilmington/animals/Cat.java +++ b/src/main/java/rocks/zipcodewilmington/animals/Cat.java @@ -10,6 +10,11 @@ public Cat(String name, Date birthDate, Integer id) { super(name, birthDate, id); } + public Cat(String name) { + super(name); + } + + public String speak() { return "meow!"; } diff --git a/src/main/java/rocks/zipcodewilmington/animals/Mammal.java b/src/main/java/rocks/zipcodewilmington/animals/Mammal.java index eec2aaa..d6c8023 100644 --- a/src/main/java/rocks/zipcodewilmington/animals/Mammal.java +++ b/src/main/java/rocks/zipcodewilmington/animals/Mammal.java @@ -12,7 +12,14 @@ public abstract class Mammal implements Animal { private final Integer id; private ArrayList eatenMeals; private String name; - private Date birthDate; + private Date birthDate; //Does this need to be private or protected? + + public Mammal(String name){ + this.name = name; + this.birthDate = null; + this.id = 0; + + } public Mammal(String name, Date birthDate, Integer id) { this.name = name; @@ -21,6 +28,7 @@ public Mammal(String name, Date birthDate, Integer id) { this.id = id; } + public String getName() { return name; } diff --git a/src/test/java/rocks/zipcodewilmington/AnimalFactoryTest.java b/src/test/java/rocks/zipcodewilmington/AnimalFactoryTest.java index 7522ce3..e48d8c9 100644 --- a/src/test/java/rocks/zipcodewilmington/AnimalFactoryTest.java +++ b/src/test/java/rocks/zipcodewilmington/AnimalFactoryTest.java @@ -1,9 +1,52 @@ package rocks.zipcodewilmington; +import org.junit.Assert; +import org.junit.Test; +import rocks.zipcodewilmington.animals.Animal; +import rocks.zipcodewilmington.animals.Cat; +import rocks.zipcodewilmington.animals.Dog; +import rocks.zipcodewilmington.animals.animal_creation.AnimalFactory; +import rocks.zipcodewilmington.animals.animal_storage.CatHouse; +import rocks.zipcodewilmington.animals.animal_storage.DogHouse; + +import java.util.Date; + /** * @author leon on 4/19/18. */ public class AnimalFactoryTest { //TODO - Create Test for `Animal createDog(String name, Date birthDate)` //TODO - Create Test for `Animal createCat(String name, Date birthDate)` + + @Test + public void createAnimalTest(){ + //given + String givenName = "Julie"; + Date givenBirthDate = new Date(); + + + //when + Animal newCat = AnimalFactory.createCat(givenName, givenBirthDate); + CatHouse.add((Cat) newCat); + + //then + int actual = CatHouse.getNumberOfCats(); + Assert.assertEquals(1, actual); + } + + @Test + public void createAnimalTest2(){ + //given + String givenName = "James"; + Date givenBirthDate = new Date(); + + + //when + Dog newCat = AnimalFactory.createDog(givenName, givenBirthDate); + DogHouse.add((Dog) newCat); //guess I can name dog newCat if I want + + //then + int actual = DogHouse.getNumberOfDogs(); + Assert.assertEquals(1, actual); + } } diff --git a/src/test/java/rocks/zipcodewilmington/CatHouseTest.java b/src/test/java/rocks/zipcodewilmington/CatHouseTest.java index f756b34..5136930 100644 --- a/src/test/java/rocks/zipcodewilmington/CatHouseTest.java +++ b/src/test/java/rocks/zipcodewilmington/CatHouseTest.java @@ -1,5 +1,13 @@ package rocks.zipcodewilmington; +import org.junit.Assert; +import org.junit.Test; +import rocks.zipcodewilmington.animals.Cat; +import rocks.zipcodewilmington.animals.Dog; +import rocks.zipcodewilmington.animals.animal_creation.AnimalFactory; +import rocks.zipcodewilmington.animals.animal_storage.CatHouse; +import rocks.zipcodewilmington.animals.animal_storage.DogHouse; + /** * @author leon on 4/19/18. */ @@ -9,4 +17,79 @@ public class CatHouseTest { // TODO - Create tests for `void remove(Cat cat)` // TODO - Create tests for `Cat getCatById(Integer id)` // TODO - Create tests for `Integer getNumberOfCats()` + + @Test + public void addCatTest(){ + //given + Cat createdCat = new Cat("Marshall"); + CatHouse catHouse = new CatHouse(); + + + //when + CatHouse.add(createdCat); + int actual = CatHouse.getNumberOfCats(); + + //then + Assert.assertEquals(1, actual); + + } + + @Test + public void removeIntegerCatTest(){ + //given + Cat createdCat = new Cat("Marshall", null, 0); + Cat createdCat2 = new Cat("Wiggles", null, 2); + Cat createdCat3 = new Cat("Lego", null, 3); + CatHouse catHouse = new CatHouse(); + //new instantiation of CatHouse() unnecessary + + //when + catHouse.add(createdCat); + catHouse.add(createdCat2); + CatHouse.add(createdCat3); + catHouse.remove(0); + + //then + int actual = CatHouse.getNumberOfCats(); + Assert.assertEquals(2, actual); + + + } + + @Test + public void getCatIdTest(){ + //given + Cat createdCat = new Cat("Marshall", null, 0); + Cat createdCat2 = new Cat("Wiggles", null, 2); + Cat createdCat3 = new Cat("Lego", null, 3); + + + //when + CatHouse.add(createdCat); + CatHouse.add(createdCat2); + CatHouse.add(createdCat3); + CatHouse.remove(0); + + //then + int actual = CatHouse.getNumberOfCats(); + Cat actualCat = CatHouse.getCatById(2); + Assert.assertEquals(2, actual); + Assert.assertEquals(createdCat2, actualCat); + + } + @Test + public void addCatTest2(){ + //given + Cat createdCat = AnimalFactory.createCat("Charzar", null); + + + + //when + CatHouse.add(createdCat); + int actual = CatHouse.getNumberOfCats(); + + //then + Assert.assertEquals(1, actual); + + } } diff --git a/src/test/java/rocks/zipcodewilmington/CatTest.java b/src/test/java/rocks/zipcodewilmington/CatTest.java index 4bd465f..b9d183e 100644 --- a/src/test/java/rocks/zipcodewilmington/CatTest.java +++ b/src/test/java/rocks/zipcodewilmington/CatTest.java @@ -2,7 +2,9 @@ import org.junit.Assert; import org.junit.Test; +import rocks.zipcodewilmington.animals.Animal; import rocks.zipcodewilmington.animals.Cat; +import rocks.zipcodewilmington.animals.Mammal; import java.util.Date; @@ -10,6 +12,22 @@ * @author leon on 4/19/18. */ public class CatTest { + + @Test + public void getNameTest(){ + //given + Cat testCat = new Cat("Charlotte"); + String expected = "Charlotte"; + + //when + String actual = testCat.getName(); + + //then + Assert.assertEquals(expected,actual); + + } + + // TODO - Create tests for `void setName(String name)` // TODO - Create tests for `speak` // TODO - Create tests for `setBirthDate(Date birthDate)` @@ -40,4 +58,125 @@ public void constructorTest() { Assert.assertEquals(givenId, retrievedId); } + @Test + public void setNameTest(){ + //given + String givenName = "Zulu"; +// Date givenBirthDate = new Date(); +// Integer givenId = 0; + String expectedName = "Shaq"; + + //when + Cat test = new Cat(givenName); + test.setName(expectedName); + + //then + String actual = test.getName(); + Assert.assertEquals(expectedName,actual); + + } + + @Test + public void setBirthDateTest(){ + //given + String givenName = "Zulu"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + Date expectedDate = new Date(21-1-2019); + + //when + Cat test = new Cat(givenName, givenBirthDate, givenId); + test.setBirthDate(expectedDate); + + //then + Date actual = test.getBirthDate(); + Assert.assertEquals(expectedDate,actual); + + } + + @Test + public void speakTest(){ + //given + String givenName = "McStuffins"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + String expected = "meow!"; + + + //when + Cat test = new Cat(givenName, givenBirthDate, givenId); + String actual = test.speak(); + + //then + Assert.assertEquals(expected,actual); + } + + @Test + public void eatTest(){ + //given + String givenName = "McStuffins"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + Integer expected = 1; + Food testFood = new Food("Kitty Chow", 4, "chicken", true); + + + //when + Cat test = new Cat(givenName, givenBirthDate, givenId); + test.eat(testFood); + + + //then + Integer actual = test.getNumberOfMealsEaten(); + Assert.assertEquals(expected,actual); + } + + @Test + public void eatTest2(){ + //given + String givenName = "McStuffins"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + Integer expected = 2; + Food testFood = new Food("Kitty Chow", 4, "chicken", true); + Food testFood2 = new Food("Purina", 2, "tuna", false); + + //when + Cat test = new Cat(givenName, givenBirthDate, givenId); + test.eat(testFood); + test.eat(testFood2); + + + //then + Integer actual = test.getNumberOfMealsEaten(); + Assert.assertEquals(expected,actual); + } + + @Test + public void getIdTest(){ + //given + String givenName = "McStuffins"; + Date givenBirthDate = new Date(); + Integer givenId = 2; + Integer expected = 2; + + //when + Cat test = new Cat(givenName, givenBirthDate, givenId); + Integer actual = test.getId(); + + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testInheritance() { + Mammal cat = new Cat("Sniffles"); + Assert.assertTrue(cat instanceof Mammal); + } + + @Test + public void testAnimalInheritance(){ + Animal cat = new Cat("Pegasus"); + Assert.assertTrue(cat instanceof Animal); + } } diff --git a/src/test/java/rocks/zipcodewilmington/DogHouseTest.java b/src/test/java/rocks/zipcodewilmington/DogHouseTest.java index b88b30b..3d46359 100644 --- a/src/test/java/rocks/zipcodewilmington/DogHouseTest.java +++ b/src/test/java/rocks/zipcodewilmington/DogHouseTest.java @@ -1,8 +1,11 @@ package rocks.zipcodewilmington; +import org.junit.Assert; import org.junit.Test; import rocks.zipcodewilmington.animals.Dog; +import rocks.zipcodewilmington.animals.Dog; import rocks.zipcodewilmington.animals.animal_creation.AnimalFactory; +import rocks.zipcodewilmington.animals.animal_storage.CatHouse; import rocks.zipcodewilmington.animals.animal_storage.DogHouse; import java.util.Date; @@ -31,4 +34,82 @@ public void testGetNumberOfDogs() { // Then DogHouse.getNumberOfDogs(); } + @Test + public void addDogTest(){ + //given + Dog createdDog = AnimalFactory.createDog("Busta", null); + + + + //when + DogHouse.add(createdDog); + int actual = DogHouse.getNumberOfDogs(); + + //then + Assert.assertEquals(1, actual); + + } + + @Test + public void removeIntegerDogTest(){ + //given + Dog createdDog = new Dog("Marshall", null, 0); + Dog createdDog2 = new Dog("Wiggles", null, 2); + Dog creaatedDog3 = new Dog("Lego", null, 3); + + + //when + DogHouse.add(createdDog); + DogHouse.add(createdDog2); + DogHouse.add(creaatedDog3); + DogHouse.remove(0); + + //then + int actual = DogHouse.getNumberOfDogs(); + Assert.assertEquals(2, actual); + + + } + + @Test + public void getDogIdTest(){ + //given + Dog createdDog = new Dog("Marshall", null, 0); + Dog createdDog2 = new Dog("Wiggles", null, 2); + Dog creaatedDog3 = new Dog("Lego", null, 3); + + + //when + DogHouse.add(createdDog); + DogHouse.add(createdDog2); + DogHouse.add(creaatedDog3); + DogHouse.remove(0); + + //then + int actual = DogHouse.getNumberOfDogs(); + Assert.assertEquals(2, actual); + Dog actualDog = DogHouse.getDogById(2); + Assert.assertEquals(createdDog2, actualDog); + } + + @Test + public void removeDogNameTest(){ + //given + Dog createdDog = new Dog("Marshall", null, 0); + Dog createdDog2 = new Dog("Wiggles", null, 2); + Dog creaatedDog3 = new Dog("Lego", null, 3); + + + //when + DogHouse.add(createdDog); + DogHouse.add(createdDog2); + DogHouse.add(creaatedDog3); + DogHouse.remove(createdDog); + + //then + int actual = DogHouse.getNumberOfDogs(); + Assert.assertEquals(2, actual); + Dog actualDog = DogHouse.getDogById(2); + Assert.assertEquals(createdDog2, actualDog); + } } diff --git a/src/test/java/rocks/zipcodewilmington/DogTest.java b/src/test/java/rocks/zipcodewilmington/DogTest.java index 34a15bd..3acbcb2 100644 --- a/src/test/java/rocks/zipcodewilmington/DogTest.java +++ b/src/test/java/rocks/zipcodewilmington/DogTest.java @@ -2,7 +2,12 @@ import org.junit.Assert; import org.junit.Test; +import rocks.zipcodewilmington.animals.Animal; +import rocks.zipcodewilmington.animals.Cat; import rocks.zipcodewilmington.animals.Dog; +import rocks.zipcodewilmington.animals.Mammal; + +import java.util.Date; /** * @author leon on 4/19/18. @@ -28,4 +33,107 @@ public void setNameTest() { String dogName = dog.getName(); Assert.assertEquals(dogName, givenName); } + + @Test + public void constructorTest(){ + //given + String name = "Orenthal"; + Date givenBirthDate = new Date(); + Integer id = 0; + + //when + Dog testDog = new Dog(name, givenBirthDate, id); + String retrievedName = testDog.getName(); + Date retrievedBirthDate = testDog.getBirthDate(); + Integer retrievedId = testDog.getId(); + + //then + Assert.assertEquals(name, retrievedName); + Assert.assertEquals(givenBirthDate, retrievedBirthDate); + Assert.assertEquals(id, retrievedId); + + } + + @Test + public void setBirthDateTest(){ + //given + String givenName = "Zulu"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + Date expectedDate = new Date(21-1-2019); + + //when + Dog test = new Dog(givenName, givenBirthDate, givenId); + test.setBirthDate(expectedDate); + + //then + Date actual = test.getBirthDate(); + Assert.assertEquals(expectedDate,actual); + + } + + @Test + public void speakTest(){ + //given + String givenName = "Bromley"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + String expected = "bark!"; + + + //when + Dog test = new Dog(givenName, givenBirthDate, givenId); + String actual = test.speak(); + + //then + Assert.assertEquals(expected,actual); + } + + @Test + public void eatTest(){ + //given + String givenName = "Mojo Jojo"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + Integer expected = 1; + Food testFood = new Food("Puppy Chow", 6, "beef", true); + + + //when + Dog test = new Dog(givenName, givenBirthDate, givenId); + test.eat(testFood); + + + //then + Integer actual = test.getNumberOfMealsEaten(); + Assert.assertEquals(expected,actual); + } + + @Test + public void getIdTest(){ + //given + String givenName = "Juice"; + Date givenBirthDate = new Date(); + Integer givenId = 2; + Integer expected = 2; + + //when + Dog test = new Dog(givenName, givenBirthDate, givenId); + Integer actual = test.getId(); + + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testInheritance() { + Mammal dog = new Dog("Snickers", null, null); + Assert.assertTrue(dog instanceof Mammal); + } + + @Test + public void testAnimalInheritance(){ + Animal dog = new Dog("Hercules", null, null); + Assert.assertTrue(dog instanceof Animal); + } }