-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathZooServiceTest.java
More file actions
212 lines (171 loc) · 7.08 KB
/
ZooServiceTest.java
File metadata and controls
212 lines (171 loc) · 7.08 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.tddacademy.zoo.service;
import com.tddacademy.zoo.model.Zoo;
import com.tddacademy.zoo.repository.ZooRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class ZooServiceTest {
@Mock
private ZooRepository zooRepository;
@InjectMocks
private ZooService zooService;
private Zoo manilaZoo;
private Zoo cebuSafari;
@BeforeEach
void setUp() {
manilaZoo = new Zoo("Manila Zoo", "Manila, Philippines", "A beautiful zoo in the heart of Manila");
cebuSafari = new Zoo("Cebu Safari", "Cebu, Philippines", "World famous safari park");
}
@Test
@DisplayName("Should get all zoos")
void shouldGetAllZoos() {
// Given
List<Zoo> expectedZoos = Arrays.asList(manilaZoo, cebuSafari);
when(zooRepository.findAll()).thenReturn(expectedZoos);
// When
List<Zoo> actualZoos = zooService.getAllZoos();
// Then
assertEquals(2, actualZoos.size());
assertEquals("Manila Zoo", actualZoos.get(0).getName());
assertEquals("Cebu Safari", actualZoos.get(1).getName());
}
@Test
@DisplayName("Should get zoo by id when exists")
void shouldGetZooByIdWhenExists() {
// Given
Long zooId = 1L;
manilaZoo.setId(zooId);
when(zooRepository.findById(zooId)).thenReturn(Optional.of(manilaZoo));
// When
Optional<Zoo> foundZoo = zooService.getZooById(zooId);
// Then
assertTrue(foundZoo.isPresent());
assertEquals("Manila Zoo", foundZoo.get().getName());
}
@Test
@DisplayName("Should return empty when zoo not found")
void shouldReturnEmptyWhenZooNotFound() {
// Given
Long zooId = 999L;
when(zooRepository.findById(zooId)).thenReturn(Optional.empty());
// When
Optional<Zoo> foundZoo = zooService.getZooById(zooId);
// Then
assertTrue(foundZoo.isEmpty());
}
@Test
@DisplayName("Should create zoo successfully")
void shouldCreateZooSuccessfully() {
// Given
manilaZoo.setId(1L);
when(zooRepository.save(any(Zoo.class))).thenReturn(manilaZoo);
// When
Zoo createdZoo = zooService.createZoo(manilaZoo);
// Then
assertNotNull(createdZoo.getId());
assertEquals("Manila Zoo", createdZoo.getName());
verify(zooRepository, times(1)).save(manilaZoo);
}
@Test
@DisplayName("Should update zoo when exists")
void shouldUpdateZooWhenExists() {
// TODO: Complete this test
// 1. Set up the existing zoo with ID 1
// 2. Create a new zoo with updated details
// 3. Mock zooRepository.findById(1L) to return the existing zoo
// 4. Mock zooRepository.save(any(Zoo.class)) to return the updated zoo
// 5. Call zooService.updateZoo(1L, updatedZoo)
// 6. Assert that the returned zoo has the updated name
// 7. Verify that zooRepository.save was called once
// Your code here:
Long zooId = 1L;
manilaZoo.setId(zooId);
Zoo updatedZoo = new Zoo("Updated Manila Zoo", "Updated Location", "Updated description");
when(zooRepository.findById(zooId)).thenReturn(Optional.of(manilaZoo));
when(zooRepository.save(any(Zoo.class))).thenReturn(updatedZoo);
Zoo result = zooService.updateZoo(zooId, updatedZoo);
assertEquals("Updated Manila Zoo", result.getName());
verify(zooRepository, times(1)).save(any(Zoo.class));
}
@Test
@DisplayName("Should throw exception when updating non-existent zoo")
void shouldThrowExceptionWhenUpdatingNonExistentZoo() {
// TODO: Complete this test
// 1. Mock zooRepository.findById(999L) to return Optional.empty()
// 2. Create a zoo with updated details
// 3. Use assertThrows to test that zooService.updateZoo(999L, updatedZoo) throws IllegalArgumentException
// 4. Verify the exception message contains "Zoo not found with id: 999"
// Your code here:
Long zooId = 999L;
Zoo updatedZoo = new Zoo("Updated Zoo", "Updated Location", "Updated description");
when(zooRepository.findById(zooId)).thenReturn(Optional.empty());
IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class,
() -> zooService.updateZoo(zooId, updatedZoo)
);
assertTrue(exception.getMessage().contains("Zoo not found with id: 999"));
}
@Test
@DisplayName("Should delete zoo when exists")
void shouldDeleteZooWhenExists() {
// TODO: Complete this test
// 1. Mock zooRepository.existsById(1L) to return true
// 2. Call zooService.deleteZoo(1L)
// 3. Verify that zooRepository.deleteById(1L) was called once
// Your code here:
Long zooId = 1L;
when(zooRepository.existsById(zooId)).thenReturn(true);
zooService.deleteZoo(zooId);
verify(zooRepository, times(1)).deleteById(zooId);
}
@Test
@DisplayName("Should throw exception when deleting non-existent zoo")
void shouldThrowExceptionWhenDeletingNonExistentZoo() {
// TODO: Complete this test
// 1. Mock zooRepository.existsById(999L) to return false
// 2. Use assertThrows to test that zooService.deleteZoo(999L) throws IllegalArgumentException
// 3. Verify the exception message contains "Zoo not found with id: 999"
// Your code here:
Long zooId = 999L;
when(zooRepository.existsById(zooId)).thenReturn(false);
IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class,
() -> zooService.deleteZoo(zooId)
);
assertTrue(exception.getMessage().contains("Zoo not found with id: 999"));
}
@Test
@DisplayName("Should find zoos by name")
void shouldFindZoosByName() {
// Given
List<Zoo> expectedZoos = Arrays.asList(manilaZoo);
when(zooRepository.findByNameContainingIgnoreCase("Manila")).thenReturn(expectedZoos);
// When
List<Zoo> foundZoos = zooService.findZoosByName("Manila");
// Then
assertEquals(1, foundZoos.size());
assertEquals("Manila Zoo", foundZoos.get(0).getName());
}
@Test
@DisplayName("Should check if zoo exists")
void shouldCheckIfZooExists() {
// Given
Long zooId = 1L;
when(zooRepository.existsById(zooId)).thenReturn(true);
when(zooRepository.existsById(999L)).thenReturn(false);
// When & Then
assertTrue(zooService.zooExists(zooId));
assertFalse(zooService.zooExists(999L));
}
}