|
23 | 23 | import java.util.stream.Stream; |
24 | 24 |
|
25 | 25 | import static org.junit.jupiter.api.Assertions.assertEquals; |
26 | | -import static org.junit.jupiter.api.Assertions.assertNotNull; |
27 | 26 | import static org.junit.jupiter.api.Assertions.assertNull; |
28 | 27 | import static org.junit.jupiter.api.Assertions.assertThrows; |
29 | 28 | import static org.junit.jupiter.api.Assertions.assertTrue; |
@@ -131,34 +130,33 @@ void testCreateBook_Success() { |
131 | 130 | BookRequest specialRequest = |
132 | 131 | new BookRequest("Java & Friends!", "Synopsis", "Author"); |
133 | 132 |
|
134 | | - Book expectedBook = |
| 133 | + // The "Future" Book (What the DB returns) |
| 134 | + Book bookFromDb = |
135 | 135 | Book.builder() |
136 | 136 | .id(testId) |
137 | 137 | .title(specialRequest.title()) |
138 | 138 | .synopsis(specialRequest.synopsis()) |
139 | 139 | .author(specialRequest.author()) |
140 | 140 | .build(); |
141 | 141 |
|
142 | | - when(testBookRepository.save(any(Book.class))).thenReturn(expectedBook); |
| 142 | + when(testBookRepository.save(any(Book.class))).thenReturn(bookFromDb); |
143 | 143 |
|
144 | 144 | // Act |
145 | 145 | Book result = testBookService.createBook(specialRequest); |
146 | 146 |
|
147 | 147 | // Assert: Verify the Service fulfills its return contract |
148 | | - assertEquals(expectedBook, result, "Service must return the object returned by the repository"); |
| 148 | + assertEquals(bookFromDb, result, "Service must return the object returned by the repository"); |
149 | 149 |
|
150 | | - // Assert: capture the Book passed to save() |
| 150 | + // Assert: Capture the "Past" Book (What went INTO the DB) |
151 | 151 | ArgumentCaptor<Book> bookCaptor = ArgumentCaptor.forClass(Book.class); |
152 | 152 | verify(testBookRepository, times(1)).save(bookCaptor.capture()); |
153 | | - Book savedBook = bookCaptor.getValue(); |
| 153 | + Book bookSentToDb = bookCaptor.getValue(); |
154 | 154 |
|
155 | 155 | // Assert |
156 | | - assertNotNull(savedBook); |
157 | | - assertNull(savedBook.getId(), "ID should be null before DB generates it"); |
158 | | - assertEquals(specialRequest.title(), savedBook.getTitle()); |
159 | | - assertEquals(specialRequest.synopsis(), savedBook.getSynopsis()); |
160 | | - assertEquals(specialRequest.author(), savedBook.getAuthor()); |
161 | | - |
| 156 | + assertNull(bookSentToDb.getId(), "ID should be null before DB generates it"); |
| 157 | + assertEquals(specialRequest.title(), bookSentToDb.getTitle()); |
| 158 | + assertEquals(specialRequest.synopsis(), bookSentToDb.getSynopsis()); |
| 159 | + assertEquals(specialRequest.author(), bookSentToDb.getAuthor()); |
162 | 160 | } |
163 | 161 |
|
164 | 162 | @Test |
|
0 commit comments