-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathPersonTest.java
More file actions
53 lines (43 loc) · 1.94 KB
/
PersonTest.java
File metadata and controls
53 lines (43 loc) · 1.94 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
import org.example.InvalidNameFormatException;
import org.example.Person;
import org.example.PersonsList;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class PersonTest {
@Test
void setAge_shouldThrow_whenAgeLessThanZero() {
Person p = new Person(1, "John Doe", 20, "Developer");
assertThrows(IllegalArgumentException.class, () -> p.setAge(-1));
}
@Test
void findByName_shouldReturnCorrectPerson_whenNameIsProperlyFormatted() {
Person p1 = new Person(1, "John Doe", 20, "Developer");
Person p2 = new Person(2, "Jane Smith", 30, "Designer");
PersonsList list = new PersonsList(List.of(p1, p2));
Person found = list.findByName("Jane Smith");
assertNotNull(found);
assertEquals(p2, found);
assertEquals(2, found.getId());
}
@Test
void findByName_shouldThrow_whenNameIsNotProperlyFormatted() {
PersonsList list = new PersonsList();
assertThrows(InvalidNameFormatException.class, () -> list.findByName("Jane"));
assertThrows(InvalidNameFormatException.class, () -> list.findByName("Jane Smith"));
assertThrows(InvalidNameFormatException.class, () -> list.findByName(" Jane Smith"));
assertThrows(InvalidNameFormatException.class, () -> list.findByName("Jane Smith "));
}
@Test
void clone_shouldCreateNewPerson_withSamePropertiesExceptNewId() {
Person original = new Person(10, "John Doe", 25, "Developer");
PersonsList list = new PersonsList(List.of(original));
Person cloned = list.clone(original);
assertNotNull(cloned);
assertNotEquals(original.getId(), cloned.getId());
assertEquals(original.getName(), cloned.getName());
assertEquals(original.getAge(), cloned.getAge());
assertEquals(original.getOccupation(), cloned.getOccupation());
assertTrue(original.equals(cloned));
}
}