Skip to content

Commit

Permalink
test: 상품 목록 조회 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
kmebin committed Nov 1, 2023
1 parent ceeda93 commit 8727fa9
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/test/java/com/moabam/api/application/ProductServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.moabam.api.application;

import static com.moabam.fixture.ProductFixture.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.util.List;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.moabam.api.domain.entity.Product;
import com.moabam.api.domain.repository.ProductRepository;
import com.moabam.api.dto.ProductResponse;
import com.moabam.api.dto.ProductsResponse;

@ExtendWith(MockitoExtension.class)
class ProductServiceTest {

@InjectMocks
ProductService productService;

@Mock
ProductRepository productRepository;

@DisplayName("상품 목록을 조회한다.")
@Test
void get_products_success() {
// given
Product product1 = bugProduct();
Product product2 = bugProduct();
given(productRepository.findAll()).willReturn(List.of(product1, product2));

// when
ProductsResponse response = productService.getProducts();

// then
List<String> productNames = response.products().stream()
.map(ProductResponse::name)
.toList();
assertThat(response.products()).hasSize(2);
assertThat(productNames).containsOnly(BUG_PRODUCT_NAME, BUG_PRODUCT_NAME);
}
}
36 changes: 36 additions & 0 deletions src/test/java/com/moabam/api/domain/entity/ProductTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.moabam.api.domain.entity;

import static org.assertj.core.api.AssertionsForClassTypes.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import com.moabam.global.error.exception.BadRequestException;

class ProductTest {

@DisplayName("상품 가격이 0 보다 작으면 예외가 발생한다.")
@Test
void validate_price_exception() {
Product.ProductBuilder productBuilder = Product.builder()
.name("X10")
.price(-10);

assertThatThrownBy(productBuilder::build)
.isInstanceOf(BadRequestException.class)
.hasMessage("가격은 0 이상이어야 합니다.");
}

@DisplayName("상품량이 1 보다 작으면 예외가 발생한다.")
@Test
void validate_quantity_exception() {
Product.ProductBuilder productBuilder = Product.builder()
.name("X10")
.price(1000)
.quantity(-1);

assertThatThrownBy(productBuilder::build)
.isInstanceOf(BadRequestException.class)
.hasMessage("수량은 1 이상이어야 합니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.moabam.api.presentation;

import static java.nio.charset.StandardCharsets.*;
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.ProductService;
import com.moabam.api.domain.entity.Product;
import com.moabam.api.dto.ProductMapper;
import com.moabam.api.dto.ProductsResponse;
import com.moabam.fixture.ProductFixture;

@SpringBootTest
@AutoConfigureMockMvc
class ProductControllerTest {

@Autowired
MockMvc mockMvc;

@Autowired
ObjectMapper objectMapper;

@MockBean
ProductService productService;

@DisplayName("상품 목록을 조회한다.")
@Test
void get_products_success() throws Exception {
// given
Product product1 = ProductFixture.bugProduct();
Product product2 = ProductFixture.bugProduct();
ProductsResponse expected = ProductMapper.toProductsResponse(List.of(product1, product2));
given(productService.getProducts()).willReturn(expected);

// when & then
String content = mockMvc.perform(get("/products"))
.andDo(print())
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString(UTF_8);
ProductsResponse actual = objectMapper.readValue(content, ProductsResponse.class);
Assertions.assertThat(actual).isEqualTo(expected);
}
}
20 changes: 20 additions & 0 deletions src/test/java/com/moabam/fixture/ProductFixture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.moabam.fixture;

import com.moabam.api.domain.entity.Product;
import com.moabam.api.domain.entity.enums.ProductType;

public class ProductFixture {

public static final String BUG_PRODUCT_NAME = "X10";
public static final int BUG_PRODUCT_PRICE = 3000;
public static final int BUG_PRODUCT_QUANTITY = 10;

public static Product bugProduct() {
return Product.builder()
.type(ProductType.BUG)
.name(BUG_PRODUCT_NAME)
.price(BUG_PRODUCT_PRICE)
.quantity(BUG_PRODUCT_QUANTITY)
.build();
}
}

0 comments on commit 8727fa9

Please sign in to comment.