1111import org .junit .jupiter .params .ParameterizedTest ;
1212import org .junit .jupiter .params .provider .Arguments ;
1313import org .junit .jupiter .params .provider .MethodSource ;
14+ import org .junit .jupiter .params .provider .NullAndEmptySource ;
15+ import org .junit .jupiter .params .provider .ValueSource ;
1416import org .mockito .ArgumentCaptor ;
1517import org .mockito .InjectMocks ;
1618import org .mockito .Mock ;
@@ -171,51 +173,6 @@ void testCreateBook_NullRequest_ThrowsException() {
171173 );
172174 }
173175
174- @ Test
175- void testCreateBook_NullTitle_ThrowsException () {
176- // Arrange
177- BookRequest invalidRequest = new BookRequest (null , "Synopsis" , "Author" );
178-
179- // Act & Assert
180- assertThrows (
181- IllegalArgumentException .class ,
182- () -> {
183- testBookService .createBook (invalidRequest );
184- }
185- );
186-
187- // Verify repository was never called
188- verify (testBookRepository , never ()).save (any ());
189- }
190-
191- @ Test
192- void testCreateBook_EmptyTitle_ThrowsException () {
193- // Arrange
194- BookRequest invalidRequest = new BookRequest ("" , "Synopsis" , "Author" );
195-
196- // Act & Assert
197- assertThrows (
198- IllegalArgumentException .class ,
199- () -> {
200- testBookService .createBook (invalidRequest );
201- }
202- );
203- }
204-
205- @ Test
206- void testCreateBook_BlankTitle_ThrowsException () {
207- // Arrange
208- BookRequest invalidRequest = new BookRequest (" " , "Synopsis" , "Author" );
209-
210- // Act & Assert
211- assertThrows (
212- IllegalArgumentException .class ,
213- () -> {
214- testBookService .createBook (invalidRequest );
215- }
216- );
217- }
218-
219176 @ Test
220177 void testCreateBook_RepositoryFailure_ThrowsException () {
221178 // Arrange
@@ -231,6 +188,19 @@ void testCreateBook_RepositoryFailure_ThrowsException() {
231188 );
232189 }
233190
191+ @ ParameterizedTest
192+ @ NullAndEmptySource // Covers Null and "" (Empty)
193+ @ ValueSource (strings = {" " , "\t " , "\n " }) // Covers Blank (Spaces/Tabs)
194+ void testCreateBook_InvalidTitle_ThrowsException (String invalidTitle ) {
195+ // Arrange
196+ BookRequest invalidRequest = new BookRequest (invalidTitle , "Synopsis" , "Author" );
197+
198+ // Act & Assert
199+ assertThrows (IllegalArgumentException .class , () -> {
200+ testBookService .createBook (invalidRequest );
201+ });
202+ }
203+
234204 // ----- EDGE cases ---------
235205
236206 @ ParameterizedTest (name = "{0}" ) // Display the test name
0 commit comments