Skip to content
This repository was archived by the owner on Aug 13, 2022. It is now read-only.

[#72] 테스트코드 mock 객체 적용 및 설정파일 분리 #75

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
/* Junit, Hamcrest, Mockito 포함하는 스프링 어플리케이션을 테스트 가능하도록 합니다. */
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'com.h2database:h2'
testImplementation("org.springframework.cloud:spring-cloud-starter-contract-stub-runner")
}

Expand Down Expand Up @@ -107,7 +108,8 @@ jacocoTestCoverageVerification {
'com.flab.doorrush.domain.**.domain.*',
'com.flab.doorrush.domain.**.exception.*',
'com.flab.doorrush.global.Response.*',
'com.flab.doorrush.global.exception.*'
'com.flab.doorrush.global.exception.*',
'com.flab.doorrush.global.dto.*'
]

limit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class OrderHistory {
private Long amount;
private OrderStatus orderStatus;
private String menuName;
private String menuPrice;
private String menuCount;
private Long menuPrice;
private int menuCount;
private LocalDateTime orderTime;

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ private void addOrderMenu(List<MenuDTO> menus, Long orderSeq) {
}

public OrderMenusCartResponse getTotalPrice(List<MenuDTO> menus) {
List<OrderMenuPrice> orderMenuCarts = new ArrayList<>();
menus.forEach(menu -> orderMenuCarts.add(orderMapper.selectPriceByMenuDTO(menu)
List<OrderMenuPrice> orderMenuPrices = new ArrayList<>();
menus.forEach(menu -> orderMenuPrices.add(orderMapper.selectPriceByMenuDTO(menu)
.orElseThrow(() -> new OrderException("메뉴 정보를 확인해주세요."))));

Long totalPrice = orderMenuCarts.stream()
Long totalPrice = orderMenuPrices.stream()
.map(OrderMenuPrice::getMenuSumPrice)
.mapToLong(Math::toIntExact)
.sum();

return OrderMenusCartResponse.builder()
.orderMenuCarts(orderMenuCarts)
.orderMenuCarts(orderMenuPrices)
.totalPrice(totalPrice)
.build();
}
Expand Down
20 changes: 13 additions & 7 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
# YAML 파일은 사람이 쉽게 읽을 수 있는 데이터 직렬화 양식이다.
# properties 보다 가독성이 좋다.

# Spring
spring:
datasource:
url: jdbc:mysql://${MYSQL_URL}:${MYSQL_PORT}/${MYSQL_DBNAME}?serverTimezone=UTC&characterEncoding=UTF-8
username: ${MYSQL_USERNAME}
password: ${MYSQL_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver

profiles:
active: local
# spring.sql.init.mode : 유형에 관계없이 항상 SQL 데이터베이스를 초기화
sql:
init:
Expand Down Expand Up @@ -41,3 +36,14 @@ resilience4j.circuitbreaker:
instances:
kakaoAddressApiCircuitBreaker:
baseConfig: default

--- # local 환경
spring:
config:
activate:
on-profile: local
datasource:
url: jdbc:mysql://${MYSQL_URL}:${MYSQL_PORT}/${MYSQL_DBNAME}?serverTimezone=UTC&characterEncoding=UTF-8
username: ${MYSQL_USERNAME}
password: ${MYSQL_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
10 changes: 5 additions & 5 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ VALUES ('1', '1', '중식', 'Y', '중식중 최고집', '증식집 중 최고를
INSERT INTO `ORDER` (ORDER_SEQ, USER_SEQ, ADDRESS, RESTAURANT_SEQ, RESTAURANT_NAME, ORDER_STATUS,
AMOUNT,
ORDER_TIME)
VALUES ('1', '25', '경기도 남양주 123-12', 3, '칸지돈부리', 2, 17500, NOW()),
('2', '25', '서울특별시 강남구 55-8', 3, '칸지돈부리', 2, 18500, NOW()),
('3', '25', '분당구 불정로 66', 3, '칸지돈부리', 2, 19500, NOW()),
('4', '23', '분당구 불정로 66', 1, '중식중 최고집', 2, 20500, NOW()),
('5', '23', '분당구 불정로 66', 1, '중식중 최고집', 1, 33500, NOW());
VALUES ('1', '25', '경기도 남양주 123-12', 3, '칸지돈부리', 'READY', 17500, NOW()),
('2', '25', '서울특별시 강남구 55-8', 3, '칸지돈부리', 'READY', 18500, NOW()),
('3', '25', '분당구 불정로 66', 3, '칸지돈부리', 'READY', 19500, NOW()),
('4', '23', '분당구 불정로 66', 1, '중식중 최고집', 'READY', 20500, NOW()),
('5', '23', '분당구 불정로 66', 1, '중식중 최고집', 'READY', 33500, NOW());

INSERT INTO `MENU` (`MENU_SEQ`, `RESTAURANT_SEQ`, `NAME`, `PRICE`)
VALUES ('1', '1', '짜장면', 7500),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.flab.doorrush.domain.order.api;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import static org.assertj.core.api.Assertions.assertThat;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.flab.doorrush.domain.order.dto.request.MenuDTO;
import com.flab.doorrush.domain.order.dto.request.OrderRequest;
Expand All @@ -25,12 +25,14 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

@SpringBootTest
@AutoConfigureMockMvc
@Transactional
@ActiveProfiles("test")
class OrderControllerTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import com.flab.doorrush.domain.order.dto.response.OrderHistory;
import com.flab.doorrush.domain.order.dto.response.OrderMenusCartResponse;
import com.flab.doorrush.domain.order.exception.OrderException;
import com.flab.doorrush.domain.restaurant.dao.RestaurantMapper;
import com.flab.doorrush.domain.user.dao.UserMapper;
import com.flab.doorrush.domain.user.exception.NotExistsAddressException;
import com.flab.doorrush.domain.user.service.UserAddressService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -19,10 +22,12 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;

@SpringBootTest
@Transactional
@ActiveProfiles("test")
class OrderServiceTest {

@Autowired
Expand All @@ -31,8 +36,18 @@ class OrderServiceTest {
@Autowired
OrderMapper orderMapper;

@Autowired
UserMapper userMapper;

@Autowired
RestaurantMapper restaurantMapper;

@Autowired
UserAddressService userAddressService;

List<MenuDTO> orderMenus;
OrderRequest orderRequest;
List<MenuDTO> list;

@BeforeEach
public void setUp() {
Expand All @@ -46,10 +61,14 @@ public void setUp() {

orderRequest = OrderRequest.builder()
.menus(orderMenus)
.addressSeq(1L)
.addressSeq(10L)
.restaurantSeq(3L)
.amount(60500L)
.build();

list = Arrays.asList(
(new MenuDTO(1L, 2)),
(new MenuDTO(2L, 3)));
}

@Test
Expand All @@ -68,10 +87,11 @@ public void createOrderSuccessTest() {
@Test
@DisplayName("메뉴정보가 비어있을 경우 OrderException 예외 발생")
public void createOrderFailTest() {
List<MenuDTO> list = new ArrayList<>();

List<MenuDTO> menuList = new ArrayList<>();
//Given
OrderRequest orderRequest = OrderRequest.builder()
.menus(list)
.menus(menuList)
.addressSeq(10L)
.restaurantSeq(3L)
.amount(60500L)
Expand Down Expand Up @@ -103,11 +123,7 @@ public void createOrderAddressFailTest() {
@Test
@DisplayName("메뉴별 금액 및 총 금액 조회 성공 테스트")
public void getTotalPrice() {
// Given
List<MenuDTO> list = new ArrayList<>();
list = Arrays.asList(
(new MenuDTO(1L, 2)),
(new MenuDTO(2L, 3)));

// When
OrderMenusCartResponse response = orderService.getTotalPrice(list);

Expand All @@ -117,4 +133,4 @@ public void getTotalPrice() {
assertThat(response.getOrderMenuCarts().get(0).getMenuSumPrice()).isEqualTo(15000L);
assertThat(response.getOrderMenuCarts().get(1).getMenuSumPrice()).isEqualTo(25500L);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -43,6 +44,7 @@
@SpringBootTest
@AutoConfigureMockMvc
@Transactional
@ActiveProfiles("test")
class UserControllerTest {

@Autowired
Expand Down Expand Up @@ -118,6 +120,7 @@ public void joinUserFailTest() throws Exception {
.andExpect(status().isBadRequest())
.andDo(print());
}

@Test
@DisplayName("비밀번호 변경 성공 테스트 200을 반환")
public void changePasswordSuccessTest() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.flab.doorrush.domain.user.dao;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.flab.doorrush.domain.restaurant.dto.request.RestaurantAddressRequest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("test")
class UserAddressMapperTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;

@SpringBootTest
@Transactional
@ActiveProfiles("test")
class UserMapperTest {

@Autowired
Expand Down
Loading