|
| 1 | +package com.example.springrest.controllers; |
| 2 | + |
| 3 | +import com.example.springrest.mappers.ProductMapperImpl; |
| 4 | +import com.example.springrest.models.Product; |
| 5 | +import com.example.springrest.services.ProductService; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 9 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 10 | +import org.springframework.context.annotation.Import; |
| 11 | +import org.springframework.test.web.servlet.MockMvc; |
| 12 | + |
| 13 | +import java.math.BigDecimal; |
| 14 | + |
| 15 | +import static org.mockito.Mockito.when; |
| 16 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 17 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 18 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 19 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 20 | + |
| 21 | +/** |
| 22 | + * Unit tests for {@link ProductController} using Spring's {@link WebMvcTest} support. |
| 23 | + * |
| 24 | + * <p><strong>Test strategy:</strong> |
| 25 | + * <ul> |
| 26 | + * <li>Start only the web layer (no database or services).</li> |
| 27 | + * <li>Inject a {@link MockMvc} client to simulate HTTP calls.</li> |
| 28 | + * <li>Replace the real {@link ProductService} with a Mockito mock using {@link MockBean}.</li> |
| 29 | + * <li>Import the {@link ProductMapperImpl} so that real mapping logic is used in tests.</li> |
| 30 | + * </ul> |
| 31 | + * |
| 32 | + * <p><strong>Note on @MockBean:</strong> |
| 33 | + * In Spring Boot 3.5+, {@link MockBean} is marked <em>deprecated</em>. |
| 34 | + * It still works, but the Spring team is encouraging a shift toward |
| 35 | + * alternative mocking approaches (e.g., plain Mockito with manual injection, |
| 36 | + * or newer testing features like {@code @ServiceTest} in future releases). |
| 37 | + * For the purposes of this book (Part 1), we continue to use {@link MockBean} |
| 38 | + * for its simplicity. We will revisit alternatives in Part 3 when covering |
| 39 | + * advanced testing strategies. |
| 40 | + * |
| 41 | + * <p><strong>Covered cases:</strong> |
| 42 | + * <ul> |
| 43 | + * <li>{@link #getByIdReturnsProduct()} — verifies that GET by ID returns the expected JSON.</li> |
| 44 | + * <li>{@link #createValidationFail()} — verifies that invalid input is rejected with 400 and error details.</li> |
| 45 | + * </ul> |
| 46 | + */ |
| 47 | +@WebMvcTest(ProductController.class) |
| 48 | +@Import(ProductMapperImpl.class) |
| 49 | +public class ProductControllerTest { |
| 50 | + @Autowired private MockMvc mvc; |
| 51 | + @MockBean private ProductService service; |
| 52 | + |
| 53 | + @Test |
| 54 | + void getByIdReturnsProduct() throws Exception { |
| 55 | + Product prod = new Product("X", BigDecimal.ONE); |
| 56 | + prod.setId(100L); |
| 57 | + when(service.getOrThrow(100L)).thenReturn(prod); |
| 58 | + mvc.perform(get("/api/products/100")) |
| 59 | + .andExpect(status().isOk()) |
| 60 | + .andExpect(jsonPath("$.id").value(100)) |
| 61 | + .andExpect(jsonPath("$.name").value("X")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void createValidationFail() throws Exception { |
| 66 | + String json = "{\"name\": \"\", \"price\": 0}"; |
| 67 | + mvc.perform(post("/api/products").contentType("application/json").content(json)) |
| 68 | + .andExpect(status().isBadRequest()) |
| 69 | + .andExpect(jsonPath("$.name").exists()) |
| 70 | + .andExpect(jsonPath("$.price").exists()); |
| 71 | + } |
| 72 | +} |
0 commit comments