77import org .junit .jupiter .api .BeforeEach ;
88import org .junit .jupiter .api .Test ;
99import org .junit .jupiter .api .extension .ExtendWith ;
10+ import org .junit .jupiter .params .provider .Arguments ;
1011import org .mockito .InjectMocks ;
1112import org .mockito .Mock ;
1213import org .mockito .MockitoAnnotations ;
1314import org .mockito .junit .jupiter .MockitoExtension ;
1415import org .springframework .util .Assert ;
1516import org .junit .jupiter .params .ParameterizedTest ;
1617import org .junit .jupiter .params .provider .CsvSource ;
18+ import org .junit .jupiter .params .provider .MethodSource ;
19+ import org .junit .jupiter .params .provider .Arguments ;
20+ import java .util .stream .Stream ;
1721
1822import java .util .Optional ;
1923import java .util .UUID ;
@@ -165,37 +169,11 @@ void testCreateBook_RepositoryReturnsNull_HandlesGracefully() {
165169
166170 // ----- EDGE cases ---------
167171
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 ) {
172+ @ ParameterizedTest (name = "{0}" ) // Display the test name
173+ @ MethodSource ("provideLongFieldTestCases" )
174+ void testCreateBook_VeryLongFields_Success (String testName , BookRequest request , Book expectedBook ) {
174175
175176 // 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- }
198-
199177 when (testBookRepository .save (any (Book .class )))
200178 .thenReturn (expectedBook );
201179
@@ -204,17 +182,95 @@ void testCreateBook_VeryLongFields_Success(int repeatCount, String fieldType, St
204182
205183 // Assert
206184 assertNotNull (result );
207- assertEquals (testId , result .getId ());
208-
209- if (fieldType .equals ("title" )) {
210- assertEquals (longText , result .getTitle ());
211- } else {
212- assertEquals (longText , result .getSynopsis ());
213- }
185+ assertEquals (expectedBook .getId (), result .getId ());
186+ assertEquals (expectedBook .getTitle (), result .getTitle ());
187+ assertEquals (expectedBook .getSynopsis (), result .getSynopsis ());
188+ assertEquals (expectedBook .getAuthor (), result .getAuthor ());
214189
215190 verify (testBookRepository , times (1 )).save (any (Book .class ));
216191 }
217192
193+ // Provide test data, static method: can be called without creating an object.
194+ private static Stream <Arguments > provideLongFieldTestCases () {
195+ UUID testId = UUID .randomUUID ();
196+
197+ String longTitle = "A" .repeat (500 );
198+ String longSynopsis = "A" .repeat (1000 );
199+
200+ return Stream .of (
201+ Arguments .of (
202+ "Very long title (500 chars)" ,
203+ new BookRequest (longTitle , "Synopsis" , "Author" ),
204+ Book .builder ()
205+ .id (testId )
206+ .title (longTitle )
207+ .synopsis ("Synopsis" )
208+ .author ("Author" )
209+ .build ()
210+ ),
211+ Arguments .of (
212+ "Very long synopsis (1000 chars)" ,
213+ new BookRequest ("Title" , longSynopsis , "Author" ),
214+ Book .builder ()
215+ .id (testId )
216+ .title ("Title" )
217+ .synopsis (longSynopsis )
218+ .author ("Author" )
219+ .build ()
220+ )
221+ );
222+ }
223+
224+ // @ParameterizedTest
225+ // @CsvSource({
226+ // "500, title, 'A very long title test'",
227+ // "1000, synopsis, 'A very long synopsis test'"
228+ // })
229+ // void testCreateBook_VeryLongFields_Success(int repeatCount, String fieldType, String description) {
230+ //
231+ // // Arrange
232+ // String longText = "A".repeat(repeatCount);
233+ //
234+ // BookRequest request;
235+ // Book expectedBook;
236+ //
237+ // if (fieldType.equals("title")) {
238+ // request = new BookRequest(longText, "Synopsis", "Author");
239+ // expectedBook = Book.builder()
240+ // .id(testId)
241+ // .title(longText)
242+ // .synopsis("Synopsis")
243+ // .author("Author")
244+ // .build();
245+ // } else {
246+ // request = new BookRequest("Title", longText, "Author");
247+ // expectedBook = Book.builder()
248+ // .id(testId)
249+ // .title("Title")
250+ // .synopsis(longText)
251+ // .author("Author")
252+ // .build();
253+ // }
254+ //
255+ // when(testBookRepository.save(any(Book.class)))
256+ // .thenReturn(expectedBook);
257+ //
258+ // // Act
259+ // Book result = testBookService.createBook(request);
260+ //
261+ // // Assert
262+ // assertNotNull(result);
263+ // assertEquals(testId, result.getId());
264+ //
265+ // if (fieldType.equals("title")) {
266+ // assertEquals(longText, result.getTitle());
267+ // } else {
268+ // assertEquals(longText, result.getSynopsis());
269+ // }
270+ //
271+ // verify(testBookRepository, times(1)).save(any(Book.class));
272+ // }
273+
218274// @Test
219275// void testCreateBook_VeryLongTitle_Success() {
220276// // Arrange
0 commit comments