Skip to content

Commit

Permalink
[#24] test: 상품 목록 조회 컨트롤러 테스트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdqls1200 committed Jun 27, 2023
1 parent e42e8cf commit 89562bf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public class ProductController {
private final ProductService productService;

@GetMapping("/products")
public ResponseEntity<ApiResponse> getProducts(@RequestParam String category) {
public ResponseEntity<ApiResponse<List<ResponseProduct>>> getProducts(@RequestParam String category) {
List<ResponseProduct> products = productService.getProducts(category);

return ResponseEntity.ok(ApiResponse.success("200", products));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package team.five.kiosk.controller;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import team.five.kiosk.dto.ResponseProduct;
import team.five.kiosk.service.ProductService;

import java.util.List;

import static org.mockito.BDDMockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest
class ProductControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private ProductService productService;

@Test
public void getProductsTest() throws Exception {
//given
final List<ResponseProduct> responseProducts = List.of(
ResponseProduct.builder().id(1L).name("아메리카노").price(4000).imageUrl("url1").build(),
ResponseProduct.builder().id(2L).name("카페라떼").price(4500).imageUrl("url2").build(),
ResponseProduct.builder().id(3L).name("바닐라라떼").price(4500).imageUrl("url3").build()
);

given(productService.getProducts(anyString())).willReturn(responseProducts);

//when
final ResultActions result = mockMvc.perform(get("/api/v1/products")
.queryParam("category", "coffee"))
.andExpect(status().isOk()).andDo(print());

//then
result.andExpect(jsonPath("status").isString());
result.andExpect(jsonPath("status").value(200));
result.andExpect(jsonPath("message[0].id").value(1L));
result.andExpect(jsonPath("message[1].id").value(2L));
}
}









0 comments on commit 89562bf

Please sign in to comment.