-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactTest.java
More file actions
147 lines (108 loc) · 6.21 KB
/
Copy pathContactTest.java
File metadata and controls
147 lines (108 loc) · 6.21 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package m3;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class ContactTest {
@Test
public void testContactId() {
// test that null ID throws an exception
Exception nullException = assertThrows(IllegalArgumentException.class, () -> {
new Contact(null, "firstName", "lastName", "1234567890", "address");
});
String nullExpectedMessage = "Contact ID must not be null and must be no longer than " + Contact.NAME_NUMBER_ID_LENGTH + " characters";
String nullActualMessage = nullException.getMessage();
assertTrue(nullActualMessage.equals(nullExpectedMessage));
// test that 11+ character contact ID throws an exception
Exception lengthException = assertThrows(IllegalArgumentException.class, () -> {
new Contact("elevenChars", "firstName", "lastName", "1234567890", "address");
});
String lengthExpectedMessage = "Contact ID must not be null and must be no longer than " + Contact.NAME_NUMBER_ID_LENGTH + " characters";
String lengthActualMessage = lengthException.getMessage();
assertTrue(lengthActualMessage.equals(lengthExpectedMessage));
// test that updating contact ID throws an exception
IllegalAccessError updateException = assertThrows(IllegalAccessError.class, () -> {
Contact testContact = new Contact("1234", "firstName", "lastName", "1234567890", "address");
testContact.setContactId("newId");
});
String updateExpectedMessage = "Contact ID cannot be changed";
String updateActualMessage = updateException.getMessage();
assertTrue(updateActualMessage.equals(updateExpectedMessage));
}
@Test
public void testFirstName() {
// test that null first name throws an exception
Exception nullException = assertThrows(IllegalArgumentException.class, () -> {
new Contact("1234567890", null, "lastName", "1234567890", "address");
});
String nullExpectedMessage = "First name must not be null and must be no longer than " + Contact.NAME_NUMBER_ID_LENGTH + " characters";
String nullActualMessage = nullException.getMessage();
assertTrue(nullActualMessage.equals(nullExpectedMessage));
// test that 11+ character first name throws an exception
Exception lengthException = assertThrows(IllegalArgumentException.class, () -> {
new Contact("1234567890", "elevenChars", "lastName", "1234567890", "address");
});
String lengthExpectedMessage = "First name must not be null and must be no longer than " + Contact.NAME_NUMBER_ID_LENGTH + " characters";
String lengthActualMessage = lengthException.getMessage();
assertTrue(lengthActualMessage.equals(lengthExpectedMessage));
}
@Test
public void testLastName() {
// test that null last name throws an exception
Exception nullException = assertThrows(IllegalArgumentException.class, () -> {
new Contact("1234567890", "firstName", null, "1234567890", "address");
});
String nullExpectedMessage = "Last name must not be null and must be no longer than " + Contact.NAME_NUMBER_ID_LENGTH + " characters";
String nullActualMessage = nullException.getMessage();
assertTrue(nullActualMessage.equals(nullExpectedMessage));
// test that 11+ character last name throws an exception
Exception lengthException = assertThrows(IllegalArgumentException.class, () -> {
new Contact("1234567890", "firstName", "elevenChars", "1234567890", "address");
});
String lengthExpectedMessage = "Last name must not be null and must be no longer than " + Contact.NAME_NUMBER_ID_LENGTH + " characters";
String lengthActualMessage = lengthException.getMessage();
assertTrue(lengthActualMessage.equals(lengthExpectedMessage));
}
@Test
public void testPhoneNumber() {
// test that null phone number throws an exception
Exception nullException = assertThrows(IllegalArgumentException.class, () -> {
new Contact("1234567890", "firstName", "lastName", null, "address");
});
String nullExpectedMessage = "Phone number must not be null and must be no longer than " + Contact.NAME_NUMBER_ID_LENGTH + " characters";
String nullActualMessage = nullException.getMessage();
assertTrue(nullActualMessage.equals(nullExpectedMessage));
// test that 11+ character phone number throws an exception
Exception lengthException = assertThrows(IllegalArgumentException.class, () -> {
new Contact("1234567890", "firstName", "lastName", "elevenChars", "address");
});
String lengthExpectedMessage = "Phone number must not be null and must be no longer than " + Contact.NAME_NUMBER_ID_LENGTH + " characters";
String lengthActualMessage = lengthException.getMessage();
assertTrue(lengthActualMessage.equals(lengthExpectedMessage));
}
@Test
public void testAddress() {
// test that null address throws an exception
Exception nullException = assertThrows(IllegalArgumentException.class, () -> {
new Contact("1234567890", "firstName", "lastName", "1234567890", null);
});
String nullExpectedMessage = "Address must not be null and must be no longer than " + Contact.ADDRESS_LENGTH + " characters";
String nullActualMessage = nullException.getMessage();
assertTrue(nullActualMessage.equals(nullExpectedMessage));
// test that 31+ character address throws an exception
Exception lengthException = assertThrows(IllegalArgumentException.class, () -> {
new Contact("1234567890", "firstName", "lastName", "1234567890", "thirty-one characters istoomany");
});
String lengthExpectedMessage = "Address must not be null and must be no longer than " + Contact.ADDRESS_LENGTH + " characters";
String lengthActualMessage = lengthException.getMessage();
assertTrue(lengthActualMessage.equals(lengthExpectedMessage));
}
@Test
public void testGoodContact() {
// test that a good contact is instantiated correctly
Contact testContact = new Contact("1234", "firstName", "lastName", "1234567890", "address");
assertTrue(testContact.getContactId().equals("1234"));
assertTrue(testContact.getFirstName().equals("firstName"));
assertTrue(testContact.getLastName().equals("lastName"));
assertTrue(testContact.getPhoneNumber().equals("1234567890"));
assertTrue(testContact.getAddress().equals("address"));
}
}