Skip to content

Commit a2e36d3

Browse files
author
“A1-4U2T1NN”
committed
fix: changed IllegalArgumentException to custom PersonIsBaldException;
1 parent 6765a0b commit a2e36d3

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Person/Person.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ public void setHeight(double height) {
6969
this.height = height;
7070
}
7171

72-
public ArrayList<String> getHairColor() {
72+
public ArrayList<String> getHairColor() throws PersonIsBaldException {
7373
if (hairColor == null || hairColor.isEmpty()) {
7474
this.hairColor = new ArrayList<>();
7575
hairColor.add("Bald");
76-
throw new IllegalArgumentException("No hair color? This person must be bald!");
76+
throw new PersonIsBaldException("No hair color? This person must be bald!");
7777
}
7878
return hairColor;
7979
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.person;
2+
3+
class PersonIsBaldException extends Exception {
4+
public PersonIsBaldException(String message) {
5+
super(message);
6+
}
7+
}

lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/person/PersonTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.codedifferently.lesson16.person;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
53
import java.util.ArrayList;
4+
65
import org.junit.jupiter.api.Assertions;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
77
import org.junit.jupiter.api.BeforeEach;
88
import org.junit.jupiter.api.Test;
99

@@ -120,7 +120,7 @@ public void testSetHeight() {
120120
}
121121

122122
@Test
123-
public void testGetHairColor() {
123+
public void testGetHairColor() throws PersonIsBaldException {
124124
// Act
125125
ArrayList<String> actualHairColor = person.getHairColor();
126126
// Arrange
@@ -132,13 +132,13 @@ public void testGetHairColor() {
132132
}
133133

134134
@Test
135-
public void testGetHairColor__hairColorDoesNotExist() {
135+
public void testGetHairColor__hairColorDoesNotExist() throws PersonIsBaldException {
136136
// Arrange
137137
Person baldPerson = new Person("John", "Male", "Caucasian", 25, 1.75, null, true);
138138
// Act
139139
Exception exception =
140140
Assertions.assertThrows(
141-
IllegalArgumentException.class,
141+
PersonIsBaldException.class,
142142
() -> {
143143
baldPerson.getHairColor();
144144
});
@@ -155,7 +155,7 @@ public void testGetNaturalHairColor() {
155155
}
156156

157157
@Test
158-
public void testSetHairColor() {
158+
public void testSetHairColor() throws PersonIsBaldException {
159159
// Arrange
160160
ArrayList<String> expectedHairColor = new ArrayList<>();
161161
expectedHairColor.add("Brown");

0 commit comments

Comments
 (0)