Skip to content

Commit

Permalink
test: 아이템 목록 조회 Controller 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
kmebin committed Nov 4, 2023
1 parent 2b88546 commit 9e50a2b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
64 changes: 64 additions & 0 deletions src/test/java/com/moabam/api/presentation/ItemControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.moabam.api.presentation;

import static java.nio.charset.StandardCharsets.*;
import static java.util.Collections.*;
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.*;

import java.util.List;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.moabam.api.application.ItemService;
import com.moabam.api.domain.entity.Item;
import com.moabam.api.domain.entity.enums.RoomType;
import com.moabam.api.dto.ItemMapper;
import com.moabam.api.dto.ItemsResponse;
import com.moabam.fixture.ItemFixture;

@SpringBootTest
@AutoConfigureMockMvc
class ItemControllerTest {

@Autowired
MockMvc mockMvc;

@Autowired
ObjectMapper objectMapper;

@MockBean
ItemService itemService;

@DisplayName("아이템 목록을 조회한다.")
@Test
void get_items_success() throws Exception {
// given
Long memberId = 1L;
RoomType type = RoomType.MORNING;
Item item1 = ItemFixture.morningSantaSkin().build();
Item item2 = ItemFixture.morningKillerSkin().build();
ItemsResponse expected = ItemMapper.toItemsResponse(List.of(item1, item2), emptyList());
given(itemService.getItems(memberId, type)).willReturn(expected);

// expected
String content = mockMvc.perform(get("/items")
.param("type", RoomType.MORNING.name()))
.andDo(print())
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString(UTF_8);
ItemsResponse actual = objectMapper.readValue(content, ItemsResponse.class);
Assertions.assertThat(actual).isEqualTo(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void get_products_success() throws Exception {
ProductsResponse expected = ProductMapper.toProductsResponse(List.of(product1, product2));
given(productService.getProducts()).willReturn(expected);

// when & then
// expected
String content = mockMvc.perform(get("/products"))
.andDo(print())
.andExpect(status().isOk())
Expand Down

0 comments on commit 9e50a2b

Please sign in to comment.