Skip to content

Commit 3f68607

Browse files
committed
feat: modified tests
1 parent 1631631 commit 3f68607

File tree

3 files changed

+47
-70
lines changed

3 files changed

+47
-70
lines changed

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/HummadTanweer/HobbyLimitExceededException.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.codedifferently.lesson16.HummadTanweer;
2+
23
import java.util.ArrayList;
34

45
public class Person {
@@ -9,6 +10,12 @@ public class Person {
910
private final String email;
1011
private final int MAX_HOBBIES = 2;
1112

13+
public class HobbyLimitExceededException extends Exception {
14+
public HobbyLimitExceededException(String message) {
15+
super(message);
16+
}
17+
}
18+
1219
enum Gender {
1320
MALE,
1421
FEMALE,
@@ -23,28 +30,17 @@ public Person(String name, int age, Gender gender, String email) {
2330
this.hobbies = new ArrayList<>();
2431
}
2532

26-
public void addHobby(String hobby) throws HobbyLimitExceededException {
33+
public void addHobby(String hobby) throws HobbyLimitExceededException {
2734
if (hobbies.size() >= MAX_HOBBIES) {
28-
throw new HobbyLimitExceededException("Cannot add more than " + MAX_HOBBIES + " hobbies.");
35+
throw new HobbyLimitExceededException("Cannot add more than " + MAX_HOBBIES + " hobbies.");
2936
}
3037
hobbies.add(hobby);
31-
}
38+
}
3239

3340
public ArrayList<String> getHobbies() {
3441
return hobbies;
3542
}
3643

37-
public void displayHobbies() {
38-
if (hobbies.isEmpty()) {
39-
System.out.println(name + " has no hobbies.");
40-
} else {
41-
System.out.println(name + "'s hobbies:");
42-
for (String hobby : hobbies) {
43-
System.out.println("- " + hobby);
44-
}
45-
}
46-
}
47-
4844
public boolean isAdult() {
4945
return age >= 18;
5046
}
@@ -53,31 +49,7 @@ public String getName() {
5349
return name;
5450
}
5551

56-
public Gender getGender() {
57-
return gender;
58-
}
59-
6052
public String getEmail() {
6153
return email;
6254
}
63-
64-
public static void main(String[] args) {
65-
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
66-
67-
try {
68-
person.addHobby("Football");
69-
person.addHobby("Pickleball");
70-
person.addHobby("Basketball");
71-
} catch (HobbyLimitExceededException e) {
72-
System.out.println(e.getMessage());
73-
}
74-
75-
person.displayHobbies();
76-
77-
if (person.isAdult()) {
78-
System.out.println(person.getName() + " Adult");
79-
} else {
80-
System.out.println(person.getName() + " Not an adult");
81-
}
82-
}
8355
}

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,55 @@
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66
import static org.junit.jupiter.api.Assertions.fail;
7-
import org.junit.jupiter.api.Test;
87

98
import com.codedifferently.lesson16.HummadTanweer.Person.Gender;
9+
import com.codedifferently.lesson16.HummadTanweer.Person.HobbyLimitExceededException;
10+
import org.junit.jupiter.api.Test;
1011

1112
public class PersonTest {
1213
@Test
1314
void testIsAdult() {
1415
Person adult = new Person("xyz", 29, Gender.MALE, "[email protected]");
1516
assertTrue(adult.isAdult(), "True for 29");
16-
1717
Person minor = new Person("abc", 15, Gender.FEMALE, "[email protected]");
1818
assertFalse(minor.isAdult(), "False for 15");
1919
}
2020

21-
@Test
22-
void testAddHobby() throws HobbyLimitExceededException {
23-
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
24-
25-
person.addHobby("Football");
26-
person.addHobby("Pickleball");
27-
28-
assertEquals(2, person.getHobbies().size());
29-
assertTrue(person.getHobbies().contains("Football"));
30-
assertTrue(person.getHobbies().contains("Pickleball"));
31-
}
21+
@Test
22+
void testAddHobby() throws HobbyLimitExceededException {
23+
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
3224

33-
@Test
34-
void testAddHobbyExceedsLimit() throws HobbyLimitExceededException {
35-
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
36-
37-
try {
38-
person.addHobby("Football");
39-
person.addHobby("Pickleball");
40-
person.addHobby("Basketball");
41-
fail("Expected HobbyLimitExceededException to be thrown");
42-
} catch (HobbyLimitExceededException e) {
43-
assertEquals("Cannot add more than 2 hobbies.", e.getMessage());
44-
}
25+
person.addHobby("Football");
26+
person.addHobby("Pickleball");
27+
28+
assertEquals(2, person.getHobbies().size());
29+
assertTrue(person.getHobbies().contains("Football"));
30+
assertTrue(person.getHobbies().contains("Pickleball"));
31+
}
32+
33+
@Test
34+
public void testGetName() {
35+
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
36+
assertEquals("xyz", person.getName());
37+
}
38+
39+
@Test
40+
public void getEmail() {
41+
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
42+
assertEquals("[email protected]", person.getEmail());
43+
}
44+
45+
@Test
46+
void testAddHobbyExceedsLimit() throws HobbyLimitExceededException {
47+
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
48+
49+
try {
50+
person.addHobby("Football");
51+
person.addHobby("Pickleball");
52+
person.addHobby("Basketball");
53+
fail("Expected HobbyLimitExceededException to be thrown");
54+
} catch (HobbyLimitExceededException e) {
55+
assertEquals("Cannot add more than 2 hobbies.", e.getMessage());
4556
}
57+
}
4658
}

0 commit comments

Comments
 (0)