forked from codesquad-members-2022/sidedish
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feat: Spring Rest Docs 의존성 추가 - #14 - Gradle 버전에 맞게 Spring Rest Docs 의존성 추가 * Feat: 개발 환경, 테스트 환경 분리 - #14 - 테스트 환경을 분리하여 H2 DB 연결 * Feat: Spring Rest Docs 기능 추가 (테스트한 더미 데이터 추가되지 않음) - #14 - 테스트를 통한 API 명세를 확인하기 위해 Spring Rest Docs 추가 - 테스트한 더미 데이터는 추가되지 않는 이슈를 갖고 있음
- Loading branch information
1 parent
ff0bf27
commit 8370c1a
Showing
8 changed files
with
228 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
= Spring REST Docs | ||
:toc: left | ||
:toclevels: 2 | ||
:sectlinks: | ||
|
||
[[resources-post]] | ||
== Post | ||
|
||
[[resources-post-create]] | ||
=== Post 생성 | ||
|
||
==== HTTP request | ||
|
||
include::{snippets}/전체 기획전 데이터/http-request.adoc[] | ||
|
||
==== HTTP response | ||
|
||
include::{snippets}/전체 기획전 데이터/http-response.adoc[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
BE/src/test/java/com/example/be/controller/dish/DishControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.example.be.controller.dish; | ||
|
||
import static org.mockito.BDDMockito.given; | ||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; | ||
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; | ||
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.example.be.controller.dish.dto.PlanningDataRequest; | ||
import com.example.be.domain.dish.Badge; | ||
import com.example.be.domain.dish.DeliveryType; | ||
import com.example.be.domain.dish.DishStatus; | ||
import com.example.be.service.dish.DishService; | ||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
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.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.restdocs.RestDocumentationExtension; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
||
@AutoConfigureMockMvc | ||
@AutoConfigureRestDocs | ||
|
||
@ExtendWith(RestDocumentationExtension.class) | ||
@WebMvcTest(DishController.class) | ||
@DisplayName("API /api/dishes/* 컨트롤러 계층 단위 테스트") | ||
class DishControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private DishService dishService; | ||
|
||
// @BeforeEach | ||
// public void setUp(WebApplicationContext webApplicationContext, | ||
// RestDocumentationContextProvider restDocumentation) { | ||
// this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) | ||
// .apply(documentationConfiguration(restDocumentation)) | ||
// .build(); | ||
// } | ||
|
||
@Test | ||
void getPlanningDataTest() throws Exception { | ||
List<PlanningDataRequest> dishes = createTestData(); | ||
|
||
given(dishService.getPlanningData()) | ||
.willReturn(dishes); | ||
|
||
ResultActions result = mockMvc.perform(MockMvcRequestBuilders.get("/api/dishes")); | ||
|
||
result | ||
.andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andDo(document("sidedish", | ||
responseFields( | ||
fieldWithPath("[].title").description("title 제목 작성"), | ||
fieldWithPath("[].description").description("description 본문 작성").optional(), | ||
fieldWithPath("[].dishId").description("1"), | ||
fieldWithPath("[].name").description("이름"), | ||
fieldWithPath("[].normalPrice").description("10000"), | ||
fieldWithPath("[].salePrice").description("9000"), | ||
fieldWithPath("[].badge").description("NONE"), | ||
fieldWithPath("[].deliveryType").description("NONE"), | ||
fieldWithPath("[].thumbnail").description("naver.com"), | ||
fieldWithPath("[].dishStatus").description("OUT_OF_STOCK"), | ||
fieldWithPath("[].categoryId").description("1") | ||
) | ||
)); | ||
|
||
} | ||
|
||
private List<PlanningDataRequest> createTestData() { | ||
List<PlanningDataRequest> dishes = new ArrayList<>(); | ||
dishes.add(0, | ||
new PlanningDataRequest(1L, "이름", "설명", new BigDecimal(10000), new BigDecimal(9000), | ||
Badge.NONE, DeliveryType.NONE, "naver.com", DishStatus.OUT_OT_STOCK, 1L, | ||
"타이틀")); | ||
dishes.add(1, | ||
new PlanningDataRequest(2L, "이름2", "설명2", new BigDecimal(100002), new BigDecimal(90002), | ||
Badge.NONE, DeliveryType.NONE, "naver.com222", DishStatus.OUT_OT_STOCK, 2L, | ||
"타이틀2")); | ||
|
||
|
||
return dishes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
spring: | ||
datasource: | ||
driverClassName: org.h2.Driver | ||
url: jdbc:h2:mem:testdb | ||
username: sa | ||
sql: | ||
init: | ||
mode: always | ||
schema-locations: classpath:testDB/schema.sql | ||
# data-locations: classpath:testDB/data.sql | ||
h2: | ||
console: | ||
enabled: true | ||
path: /h2-console | ||
mvc: | ||
pathmatch: | ||
matching-strategy: ant_path_matcher | ||
|
||
logging: | ||
level: | ||
web: DEBUG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
DROP TABLE IF EXISTS dish; | ||
CREATE TABLE dish | ||
( | ||
dish_id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, | ||
name VARCHAR(255) NULL, | ||
description VARCHAR(255) NULL, | ||
normal_price DECIMAL NULL, | ||
sale_price DECIMAL NULL, | ||
badge VARCHAR(255) NULL, | ||
delivery_type VARCHAR(255) NULL, | ||
thumbnail VARCHAR(255) NULL, | ||
category_id BIGINT NOT NULL, | ||
dish_status VARCHAR(255) NULL | ||
); | ||
|
||
|
||
DROP TABLE IF EXISTS category; | ||
CREATE TABLE category | ||
( | ||
category_id BIGINT NOT NULL PRIMARY KEY, | ||
title VARCHAR(255) | ||
); | ||
|
||
DROP TABLE IF EXISTS users; | ||
CREATE TABLE users | ||
( | ||
user_id BIGINT NOT NULL PRIMARY KEY, | ||
login_id BIGINT NOT NULL, | ||
password VARCHAR(60) NOT NULL | ||
); |