@@ -122,26 +122,44 @@ void setUp() {
122122 // --------------------------------------
123123 // Tests: createBook
124124 // --------------------------------------
125+
125126 @ Test
126127 void testCreateBook_Success () {
128+ // Arrange: also checks special chars
129+ BookRequest specialRequest =
130+ new BookRequest ("Java & Friends!" , "Synopsis" , "Author" );
127131
128- // Arrange: tell the mock repository what to do when called
129- when (testBookRepository .save (any (Book .class ))).thenReturn (persistedBook );
132+ Book expectedBook =
133+ Book .builder ()
134+ .id (testId )
135+ .title (specialRequest .title ())
136+ .synopsis (specialRequest .synopsis ())
137+ .author (specialRequest .author ())
138+ .build ();
130139
131- // Act: call the service method we are testing
132- Book result = testBookService .createBook (validBookRequest );
140+ when (testBookRepository .save (any (Book .class ))).thenReturn (expectedBook );
133141
134- // Assert: Check the outcome
135- assertNotNull (result );
136- assertEquals (testId , result .getId ());
137- assertEquals (validBookRequest .title (), result .getTitle ());
138- assertEquals (validBookRequest .synopsis (), result .getSynopsis ());
139- assertEquals (validBookRequest .author (), result .getAuthor ());
142+ // Act
143+ Book result = testBookService .createBook (specialRequest );
144+
145+ // Assert: capture the Book passed to save()
146+ ArgumentCaptor <Book > bookCaptor = ArgumentCaptor .forClass (Book .class );
147+ verify (testBookRepository , times (1 )).save (bookCaptor .capture ());
148+ Book savedBook = bookCaptor .getValue ();
149+
150+ // Assert
151+ assertNotNull (savedBook );
152+ assertNull (savedBook .getId (), "ID should be null before DB generates it" );
153+ assertEquals (specialRequest .title (), savedBook .getTitle ());
154+ assertEquals (specialRequest .synopsis (), savedBook .getSynopsis ());
155+ assertEquals (specialRequest .author (), savedBook .getAuthor ());
156+
157+ // Assert: Verify the Service fulfills its return contract
158+ assertEquals (expectedBook , result , "Service must return the object returned by the repository" );
140159
141- // Did the service perform the correct action on its dependency?
142- verify (testBookRepository , times (1 )).save (any (Book .class ));
143160 }
144161
162+
145163 @ Test
146164 void testCreateBook_NullRequest_ThrowsException () {
147165 // Act & Assert
@@ -239,42 +257,6 @@ void testCreateBook_VeryLongFields_Success(
239257 verify (testBookRepository , times (1 )).save (any (Book .class ));
240258 }
241259
242- @ Test
243- void testCreateBook_SpecialCharactersInTitle_Success () {
244- // Arrange
245- BookRequest specialRequest =
246- new BookRequest ("Test: A Book! @#$%^&*()" , "Synopsis" , "Author" );
247-
248- Book expectedBook =
249- Book .builder ()
250- .id (testId )
251- .title (specialRequest .title ())
252- .synopsis (specialRequest .synopsis ())
253- .author (specialRequest .author ())
254- .build ();
255-
256- when (testBookRepository .save (any (Book .class ))).thenReturn (expectedBook );
257-
258- // Act
259- Book result = testBookService .createBook (specialRequest );
260-
261- // Assert: capture the Book passed to save()
262- ArgumentCaptor <Book > bookCaptor = ArgumentCaptor .forClass (Book .class );
263- verify (testBookRepository , times (1 )).save (bookCaptor .capture ());
264- Book savedBook = bookCaptor .getValue ();
265-
266- // Assert
267- assertNotNull (savedBook );
268- assertNull (savedBook .getId (), "ID should be null before DB generates it" );
269- assertEquals (specialRequest .title (), savedBook .getTitle ());
270- assertEquals (specialRequest .synopsis (), savedBook .getSynopsis ());
271- assertEquals (specialRequest .author (), savedBook .getAuthor ());
272-
273- // Assert: Verify the Service fulfills its return contract
274- assertEquals (expectedBook , result , "Service must return the object returned by the repository" );
275-
276- }
277-
278260 // --------------------------------------------------------------------------------------------
279261 // Tests: deleteBookById(UUID)
280262 // -------------------------------------------------------------------------------------------
0 commit comments