|
| 1 | +package com.codedifferently.lesson16.person; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import org.junit.jupiter.api.Assertions; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +public class PersonTest { |
| 11 | + |
| 12 | + Person person; |
| 13 | + |
| 14 | + @BeforeEach |
| 15 | + void setUp() { |
| 16 | + ArrayList<String> hairColor = new ArrayList<>(); |
| 17 | + hairColor.add("Black"); |
| 18 | + hairColor.add("Purple"); |
| 19 | + person = new Person("Chigazo", "Male", "African-American", 20, 1.8288, hairColor, true); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testGetName() { |
| 24 | + // Act |
| 25 | + String actualName = person.getName(); |
| 26 | + // Arrange |
| 27 | + String expectedName = "Chigazo"; |
| 28 | + // Assert |
| 29 | + assertEquals(expectedName, actualName); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testSetName() { |
| 34 | + // Arrange |
| 35 | + String expectedSet = "Austin"; |
| 36 | + // Act |
| 37 | + person.setName(expectedSet); |
| 38 | + // Assert |
| 39 | + assertEquals(expectedSet, person.getName()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testGetSex() { |
| 44 | + // Act |
| 45 | + String actualSex = person.getSex(); |
| 46 | + // Arrange |
| 47 | + String expectedSex = "Male"; |
| 48 | + // Assert(Checking the value) |
| 49 | + assertEquals(expectedSex, actualSex); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testSetSex() { |
| 54 | + // Arrange |
| 55 | + String expectedSex = "Female"; |
| 56 | + // Act |
| 57 | + person.setSex(expectedSex); |
| 58 | + // Assert |
| 59 | + assertEquals(expectedSex, person.getSex()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testGetRace() { |
| 64 | + // Act |
| 65 | + String actualRace = person.getRace(); |
| 66 | + // Arrange |
| 67 | + String expectedRace = "African-American"; |
| 68 | + // Assert(Checking the value) |
| 69 | + assertEquals(expectedRace, actualRace); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testSetRace() { |
| 74 | + // Arrange |
| 75 | + String expectedRace = "Asian"; |
| 76 | + // Act |
| 77 | + person.setRace(expectedRace); |
| 78 | + // Assert |
| 79 | + assertEquals(expectedRace, person.getRace()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testGetAge() { |
| 84 | + // Act |
| 85 | + int actualAge = person.getAge(); |
| 86 | + // Arrange |
| 87 | + int expectedAge = 20; |
| 88 | + // Assert(Checking the value) |
| 89 | + assertEquals(expectedAge, actualAge); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testSetAge() { |
| 94 | + // Arrange |
| 95 | + int expectedAge = 21; |
| 96 | + // Act |
| 97 | + person.setAge(expectedAge); |
| 98 | + // Assert |
| 99 | + assertEquals(expectedAge, person.getAge()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testGetHeight() { |
| 104 | + // Act |
| 105 | + double actualHeight = person.getHeight(); |
| 106 | + // Arrange |
| 107 | + double expectedHeight = 1.8288; |
| 108 | + // Assert(Checking the value) |
| 109 | + assertEquals(expectedHeight, actualHeight); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testSetHeight() { |
| 114 | + // Arrange |
| 115 | + double expectedHeight = 1.92024; |
| 116 | + // Act |
| 117 | + person.setHeight(expectedHeight); |
| 118 | + // Assert |
| 119 | + assertEquals(expectedHeight, person.getHeight()); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testGetHairColor() throws PersonIsBaldException { |
| 124 | + // Act |
| 125 | + ArrayList<String> actualHairColor = person.getHairColor(); |
| 126 | + // Arrange |
| 127 | + ArrayList<String> expectedHairColor = new ArrayList<>(); |
| 128 | + expectedHairColor.add("Black"); |
| 129 | + expectedHairColor.add("Purple"); |
| 130 | + // Assert |
| 131 | + assertEquals(expectedHairColor, actualHairColor); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testGetHairColor__hairColorDoesNotExist() throws PersonIsBaldException { |
| 136 | + // Arrange |
| 137 | + Person baldPerson = new Person("John", "Male", "Caucasian", 25, 1.75, null, true); |
| 138 | + // Act |
| 139 | + Exception exception = |
| 140 | + Assertions.assertThrows( |
| 141 | + PersonIsBaldException.class, |
| 142 | + () -> { |
| 143 | + baldPerson.getHairColor(); |
| 144 | + }); |
| 145 | + // Assert |
| 146 | + assertEquals("No hair color? This person must be bald!", exception.getMessage()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testGetNaturalHairColor() { |
| 151 | + ArrayList<String> expectedNaturalHairColor = new ArrayList<>(); |
| 152 | + expectedNaturalHairColor.add("Black"); |
| 153 | + ArrayList<String> actualNaturalHairColor = person.getNaturalHairColor(); |
| 154 | + assertEquals(expectedNaturalHairColor, actualNaturalHairColor); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testSetHairColor() throws PersonIsBaldException { |
| 159 | + // Arrange |
| 160 | + ArrayList<String> expectedHairColor = new ArrayList<>(); |
| 161 | + expectedHairColor.add("Brown"); |
| 162 | + // Act |
| 163 | + person.setHairColor(expectedHairColor); |
| 164 | + // Assert |
| 165 | + assertEquals(expectedHairColor, person.getHairColor()); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void testGetLifeStatus() { |
| 170 | + // Act |
| 171 | + String actualLifeStatus = person.getLifeStatus(); |
| 172 | + // Arrange |
| 173 | + String expectedHairColor = "Alive"; |
| 174 | + // Assert |
| 175 | + assertEquals(expectedHairColor, actualLifeStatus); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void testSetLifeStatus() { |
| 180 | + // Arrange |
| 181 | + boolean expectedLifeStatus = false; |
| 182 | + // Act |
| 183 | + person.setLifeStatus(expectedLifeStatus); |
| 184 | + // Assert |
| 185 | + assertEquals("Deceased", person.getLifeStatus()); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void testGetPersonInfo() { |
| 190 | + String expectedPersonInfo = |
| 191 | + "Name: Chigazo| Gender Identity: Male| Race: African-American| Age: 20| Height(In meters): 1.8288| Hair Color: [Black, Purple]| Status: Alive"; |
| 192 | + |
| 193 | + // Act |
| 194 | + String actualPersonInfo = person.getPersonInfo(); |
| 195 | + |
| 196 | + // Assert |
| 197 | + assertEquals(expectedPersonInfo, actualPersonInfo); |
| 198 | + } |
| 199 | +} |
0 commit comments