|
12 | 12 | import org.mockito.MockitoAnnotations; |
13 | 13 | import org.mockito.junit.jupiter.MockitoExtension; |
14 | 14 | import org.springframework.util.Assert; |
| 15 | +import org.junit.jupiter.params.ParameterizedTest; |
| 16 | +import org.junit.jupiter.params.provider.CsvSource; |
15 | 17 |
|
16 | 18 | import java.util.Optional; |
17 | 19 | import java.util.UUID; |
@@ -162,61 +164,112 @@ void testCreateBook_RepositoryReturnsNull_HandlesGracefully() { |
162 | 164 | } |
163 | 165 |
|
164 | 166 | // ----- EDGE cases --------- |
165 | | - @Test |
166 | | - void testCreateBook_VeryLongTitle_Success() { |
167 | | - // Arrange |
168 | | - String longTitle = "A".repeat(500); |
169 | | - BookRequest longTitleRequest = new BookRequest(longTitle, "Synopsis", "Author"); |
170 | 167 |
|
171 | | - Book expectedBook = Book.builder() |
172 | | - .id(testId) |
173 | | - .title(longTitle) |
174 | | - .synopsis("Synopsis") |
175 | | - .author("Author") |
176 | | - .build(); |
| 168 | + @ParameterizedTest |
| 169 | + @CsvSource({ |
| 170 | + "500, title, 'A very long title test'", |
| 171 | + "1000, synopsis, 'A very long synopsis test'" |
| 172 | + }) |
| 173 | + void testCreateBook_VeryLongFields_Success(int repeatCount, String fieldType, String description) { |
| 174 | + |
| 175 | + // Arrange |
| 176 | + String longText = "A".repeat(repeatCount); |
| 177 | + |
| 178 | + BookRequest request; |
| 179 | + Book expectedBook; |
| 180 | + |
| 181 | + if (fieldType.equals("title")) { |
| 182 | + request = new BookRequest(longText, "Synopsis", "Author"); |
| 183 | + expectedBook = Book.builder() |
| 184 | + .id(testId) |
| 185 | + .title(longText) |
| 186 | + .synopsis("Synopsis") |
| 187 | + .author("Author") |
| 188 | + .build(); |
| 189 | + } else { |
| 190 | + request = new BookRequest("Title", longText, "Author"); |
| 191 | + expectedBook = Book.builder() |
| 192 | + .id(testId) |
| 193 | + .title("Title") |
| 194 | + .synopsis(longText) |
| 195 | + .author("Author") |
| 196 | + .build(); |
| 197 | + } |
177 | 198 |
|
178 | 199 | when(testBookRepository.save(any(Book.class))) |
179 | 200 | .thenReturn(expectedBook); |
180 | 201 |
|
181 | 202 | // Act |
182 | | - Book result = testBookService.createBook(longTitleRequest); |
| 203 | + Book result = testBookService.createBook(request); |
183 | 204 |
|
184 | 205 | // Assert |
185 | 206 | assertNotNull(result); |
186 | 207 | assertEquals(testId, result.getId()); |
187 | | - assertEquals(longTitle, result.getTitle()); |
188 | | - assertEquals(expectedBook.getSynopsis(), result.getSynopsis()); |
189 | | - assertEquals(expectedBook.getAuthor(), result.getAuthor()); |
190 | 208 |
|
191 | | - // Did the service perform the correct action on its dependency? |
192 | | - verify(testBookRepository, times(1)).save(any(Book.class)); |
| 209 | + if (fieldType.equals("title")) { |
| 210 | + assertEquals(longText, result.getTitle()); |
| 211 | + } else { |
| 212 | + assertEquals(longText, result.getSynopsis()); |
| 213 | + } |
193 | 214 |
|
| 215 | + verify(testBookRepository, times(1)).save(any(Book.class)); |
194 | 216 | } |
195 | 217 |
|
196 | | - @Test |
197 | | - void testCreateBook_VeryLongSynopsis_Success() { |
198 | | - |
199 | | - // Arrange |
200 | | - String longSynopsis = "A".repeat(1000); |
201 | | - BookRequest longSynopsisRequest = new BookRequest("Title", longSynopsis, "Author"); |
202 | | - |
203 | | - Book expectedBook = Book.builder() |
204 | | - .id(testId) |
205 | | - .title("Title") |
206 | | - .synopsis(longSynopsis) |
207 | | - .author("Author") |
208 | | - .build(); |
209 | | - |
210 | | - when(testBookRepository.save(any(Book.class))) |
211 | | - .thenReturn(expectedBook); |
212 | | - |
213 | | - // Act |
214 | | - Book result = testBookService.createBook(longSynopsisRequest); |
215 | | - |
216 | | - // Assert |
217 | | - assertEquals(longSynopsis, result.getSynopsis()); |
218 | | - |
219 | | - } |
| 218 | +// @Test |
| 219 | +// void testCreateBook_VeryLongTitle_Success() { |
| 220 | +// // Arrange |
| 221 | +// String longTitle = "A".repeat(500); |
| 222 | +// BookRequest longTitleRequest = new BookRequest(longTitle, "Synopsis", "Author"); |
| 223 | +// |
| 224 | +// Book expectedBook = Book.builder() |
| 225 | +// .id(testId) |
| 226 | +// .title(longTitle) |
| 227 | +// .synopsis("Synopsis") |
| 228 | +// .author("Author") |
| 229 | +// .build(); |
| 230 | +// |
| 231 | +// when(testBookRepository.save(any(Book.class))) |
| 232 | +// .thenReturn(expectedBook); |
| 233 | +// |
| 234 | +// // Act |
| 235 | +// Book result = testBookService.createBook(longTitleRequest); |
| 236 | +// |
| 237 | +// // Assert |
| 238 | +// assertNotNull(result); |
| 239 | +// assertEquals(testId, result.getId()); |
| 240 | +// assertEquals(longTitle, result.getTitle()); |
| 241 | +// assertEquals(expectedBook.getSynopsis(), result.getSynopsis()); |
| 242 | +// assertEquals(expectedBook.getAuthor(), result.getAuthor()); |
| 243 | +// |
| 244 | +// // Did the service perform the correct action on its dependency? |
| 245 | +// verify(testBookRepository, times(1)).save(any(Book.class)); |
| 246 | +// |
| 247 | +// } |
| 248 | +// |
| 249 | +// @Test |
| 250 | +// void testCreateBook_VeryLongSynopsis_Success() { |
| 251 | +// |
| 252 | +// // Arrange |
| 253 | +// String longSynopsis = "A".repeat(1000); |
| 254 | +// BookRequest longSynopsisRequest = new BookRequest("Title", longSynopsis, "Author"); |
| 255 | +// |
| 256 | +// Book expectedBook = Book.builder() |
| 257 | +// .id(testId) |
| 258 | +// .title("Title") |
| 259 | +// .synopsis(longSynopsis) |
| 260 | +// .author("Author") |
| 261 | +// .build(); |
| 262 | +// |
| 263 | +// when(testBookRepository.save(any(Book.class))) |
| 264 | +// .thenReturn(expectedBook); |
| 265 | +// |
| 266 | +// // Act |
| 267 | +// Book result = testBookService.createBook(longSynopsisRequest); |
| 268 | +// |
| 269 | +// // Assert |
| 270 | +// assertEquals(longSynopsis, result.getSynopsis()); |
| 271 | +// |
| 272 | +// } |
220 | 273 |
|
221 | 274 | @Test |
222 | 275 | void testCreateBook_SpecialCharactersInTitle_Success() { |
|
0 commit comments