|
| 1 | +package de.rieckpil.blog.library; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.when; |
| 4 | + |
| 5 | +import java.awt.print.Book; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.DisplayName; |
| 8 | +import org.junit.jupiter.api.Tag; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 11 | +import org.mockito.Mockito; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 14 | +import org.springframework.test.context.ContextConfiguration; |
| 15 | +import org.springframework.test.context.aot.DisabledInAotMode; |
| 16 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 17 | +import org.springframework.test.web.servlet.ResultActions; |
| 18 | +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; |
| 19 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 20 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; |
| 21 | +import org.springframework.test.web.servlet.result.StatusResultMatchers; |
| 22 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 23 | + |
| 24 | +@ContextConfiguration(classes = {BookController.class}) |
| 25 | +@ExtendWith(SpringExtension.class) |
| 26 | +@DisabledInAotMode |
| 27 | +class BookControllerDiffblueTest { |
| 28 | + @Autowired |
| 29 | + private BookController bookController; |
| 30 | + |
| 31 | + @MockBean |
| 32 | + private BookService bookService; |
| 33 | + |
| 34 | + /** |
| 35 | + * Test {@link BookController#getBookById(Long)}. |
| 36 | + * <ul> |
| 37 | + * <li>Given {@link BookService} {@link BookService#findById(Long)} return {@link Book} (default constructor).</li> |
| 38 | + * <li>Then status {@link StatusResultMatchers#isOk()}.</li> |
| 39 | + * </ul> |
| 40 | + * <p> |
| 41 | + * Method under test: {@link BookController#getBookById(Long)} |
| 42 | + */ |
| 43 | + @Test |
| 44 | + @DisplayName("Test getBookById(Long); given BookService findById(Long) return Book (default constructor); then status isOk()") |
| 45 | + @Tag("MaintainedByDiffblue") |
| 46 | + void testGetBookById_givenBookServiceFindByIdReturnBook_thenStatusIsOk() throws Exception { |
| 47 | + // Arrange |
| 48 | + when(bookService.findById(Mockito.<Long>any())).thenReturn(new Book()); |
| 49 | + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/books/{id}", 1L); |
| 50 | + |
| 51 | + // Act and Assert |
| 52 | + MockMvcBuilders.standaloneSetup(bookController) |
| 53 | + .build() |
| 54 | + .perform(requestBuilder) |
| 55 | + .andExpect(MockMvcResultMatchers.status().isOk()) |
| 56 | + .andExpect(MockMvcResultMatchers.content().contentType("application/xml;charset=UTF-8")) |
| 57 | + .andExpect(MockMvcResultMatchers.content().string("<Book><numberOfPages>0</numberOfPages></Book>")); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Test {@link BookController#getBookById(Long)}. |
| 62 | + * <ul> |
| 63 | + * <li>Given {@link BookService} {@link BookService#findById(Long)} return {@code null}.</li> |
| 64 | + * <li>Then status {@link StatusResultMatchers#isNotFound()}.</li> |
| 65 | + * </ul> |
| 66 | + * <p> |
| 67 | + * Method under test: {@link BookController#getBookById(Long)} |
| 68 | + */ |
| 69 | + @Test |
| 70 | + @DisplayName("Test getBookById(Long); given BookService findById(Long) return 'null'; then status isNotFound()") |
| 71 | + @Tag("MaintainedByDiffblue") |
| 72 | + void testGetBookById_givenBookServiceFindByIdReturnNull_thenStatusIsNotFound() throws Exception { |
| 73 | + // Arrange |
| 74 | + when(bookService.findById(Mockito.<Long>any())).thenReturn(null); |
| 75 | + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/books/{id}", 1L); |
| 76 | + |
| 77 | + // Act |
| 78 | + ResultActions actualPerformResult = MockMvcBuilders.standaloneSetup(bookController).build().perform(requestBuilder); |
| 79 | + |
| 80 | + // Assert |
| 81 | + actualPerformResult.andExpect(MockMvcResultMatchers.status().isNotFound()); |
| 82 | + } |
| 83 | +} |
0 commit comments