-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContactServiceTest.java
124 lines (103 loc) · 3.78 KB
/
ContactServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//*********************************************************************
// File: ContactServiceTest.java
// Name: David France
// Date: 7/31/23
// Description: Class for testing ContactService.java. Tests that ID
// increments and is unique, and that Contacts can be added,
// deleted, and updated.
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class ContactServiceTest {
private ContactService contactService;
// Set up ContactService with five Contacts before each test
@BeforeEach
void setupEach() {
contactService = new ContactService();
contactService.addContact("firstname", "lastname", "1234567890", "address");
contactService.addContact("John", "Smith", "3234567980", "12 forever lane");
contactService.addContact("Jane", "Doe", "5456789123", "46 falling water ct");
contactService.addContact("Bob", "Jones", "9897654321", "345 sunset drive");
contactService.addContact("Neil", "Poundstone", "4345646784", "456 mountainview terr");
}
// Tests all Contacts are added. Duplicate values are not allowed in a HashMap, and
// this test shows all five Contacts have been added successfully, so no duplicate
// values exist.
@Test
void testAddContacts() {
assertTrue(contactService.getContact(1).getFirstName().equals("firstname"));
assertTrue(contactService.getContact(2).getPhoneNumber().equals("3234567980"));
assertTrue(contactService.getContact(3).getLastName().equals("Doe"));
assertTrue(contactService.getContact(4).getAddress().equals("345 sunset drive"));
assertTrue(contactService.getContact(5).getFirstName().equals("Neil"));
}
// Tests for successfully deleting and updating contacts
@Test
void testDeleteContact() {
Contact contact3 = contactService.getContact(3);
Contact deletedContact3 = contactService.deleteContact(3);
assertEquals(contact3, deletedContact3);
assertThrows(IllegalArgumentException.class, () -> {
contactService.getContact(3);
});
}
@Test
void testUpdateFirstName() {
contactService.updateFirstName(2, "Josephine");
assertEquals(contactService.getContact(2).getFirstName(), "Josephine");
}
@Test
void testUpdateLastName() {
contactService.updateLastName(3, "Floyd");
assertEquals(contactService.getContact(3).getLastName(), "Floyd");
}
@Test
void testUpdateAddress() {
contactService.updateAddress(5, "1899 horseshoe overlook");
assertEquals(contactService.getContact(5).getAddress(), "1899 horseshoe overlook");
}
@Test
void testUpdatePhone() {
contactService.updatePhone(1, "9894563214");
assertEquals(contactService.getContact(1).getPhoneNumber(), "9894563214");
}
// Tests for ID that doesn't exist
@Test
void testUpdateFirstNameBadId() {
assertThrows(IllegalArgumentException.class, () -> {
contactService.updateFirstName(10, "Arthur");
});
}
@Test
void testUpdateLastNameBadId() {
assertThrows(IllegalArgumentException.class, () -> {
contactService.updateLastName(10, "Morgan");
});
}
@Test
void testUpdateAddressBadId() {
assertThrows(IllegalArgumentException.class, () -> {
contactService.updateAddress(10, "New Address");
});
}
@Test
void testUpdatePhoneBadId() {
assertThrows(IllegalArgumentException.class, () -> {
contactService.updatePhone(10, "1234567890");
});
}
@Test
void testDeleteBadId() {
assertThrows(IllegalArgumentException.class, () -> {
contactService.deleteContact(10);
});
}
// Test for null HashMap
@Test
void testNullContactMap() {
ContactService contactService2 = new ContactService();
assertThrows(IllegalArgumentException.class, () -> {
contactService2.updateFirstName(1, "John");
});
}
}