Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.ResultActions;

import java.util.Map;

import static com.epages.restdocs.apispec.ResourceDocumentation.resource;
import static org.springframework.restdocs.cookies.CookieDocumentation.cookieWithName;
import static org.springframework.restdocs.cookies.CookieDocumentation.requestCookies;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -23,6 +27,55 @@ public class ComposerControllerTest extends ControllerTestSupport {
@MockitoBean
private ComposerService composerService;

@Test
void 작곡가를_생성한다() throws Exception {
// given
Map<String, Object> requestBody = Map.of(
"koreanName", "베토벤",
"englishName", "Ludwig van Beethoven",
"gender", "MALE",
"nationality", "독일",
"birthYear", 1770,
"deathYear", 1827,
"bio", "독일의 작곡가",
"era", "CLASSICAL",
"continent", "EUROPE"
);
Comment on lines +33 to +43

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

테스트의 견고함과 유지보수성을 높이기 위해, Map<String, Object>을 사용하여 요청 본문을 생성하는 대신 ComposerCreateDto를 직접 사용하는 것이 좋습니다. 이렇게 하면 ComposerCreateDto의 필드명이 변경되거나 타입이 바뀔 경우 컴파일 시점에 오류를 발견할 수 있어 테스트가 깨지는 것을 방지할 수 있습니다. 제안된 코드를 적용한 후, IDE를 사용하여 import 문을 정리하는 것을 권장합니다.

Suggested change
Map<String, Object> requestBody = Map.of(
"koreanName", "베토벤",
"englishName", "Ludwig van Beethoven",
"gender", "MALE",
"nationality", "독일",
"birthYear", 1770,
"deathYear", 1827,
"bio", "독일의 작곡가",
"era", "CLASSICAL",
"continent", "EUROPE"
);
com.daramg.server.composer.dto.ComposerCreateDto requestBody = new com.daramg.server.composer.dto.ComposerCreateDto(
"베토벤",
"Ludwig van Beethoven",
null,
com.daramg.server.composer.domain.Gender.MALE,
"독일",
(short) 1770,
(short) 1827,
"독일의 작곡가",
com.daramg.server.composer.domain.Era.CLASSICAL,
com.daramg.server.composer.domain.Continent.EUROPE
);

Cookie cookie = new Cookie(COOKIE_NAME, "access_token");

// when
ResultActions result = mockMvc.perform(post("/composers")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody))
.cookie(cookie)
);

// then
result.andExpect(status().isCreated())
.andDo(restDocsHandler.document(
resource(ResourceSnippetParameters.builder()
.tag("Composer API")
.summary("작곡가 생성")
.description("관리자가 작곡가를 생성합니다.")
.requestFields(
fieldWithPath("koreanName").type(JsonFieldType.STRING).description("한국어 이름"),
fieldWithPath("englishName").type(JsonFieldType.STRING).description("영어 이름"),
fieldWithPath("gender").type(JsonFieldType.STRING).description("성별 (MALE / FEMALE / UNKNOWN)"),
fieldWithPath("nationality").type(JsonFieldType.STRING).description("국적").optional(),
fieldWithPath("birthYear").type(JsonFieldType.NUMBER).description("출생 연도").optional(),
fieldWithPath("deathYear").type(JsonFieldType.NUMBER).description("사망 연도").optional(),
fieldWithPath("bio").type(JsonFieldType.STRING).description("소개").optional(),
fieldWithPath("era").type(JsonFieldType.STRING).description("시대 (MEDIEVAL_RENAISSANCE / BAROQUE / CLASSICAL / ROMANTIC / MODERN_CONTEMPORARY)").optional(),
fieldWithPath("continent").type(JsonFieldType.STRING).description("대륙 (ASIA / NORTH_AMERICA / EUROPE / SOUTH_AMERICA / AFRICA_OCEANIA)").optional()
)
Comment on lines +60 to +70

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

ComposerCreateDtonativeName 필드가 존재하지만, API 문서에는 누락되어 있습니다. API 사용자가 모든 필드를 알 수 있도록 문서에 추가하는 것이 좋습니다.

Suggested change
.requestFields(
fieldWithPath("koreanName").type(JsonFieldType.STRING).description("한국어 이름"),
fieldWithPath("englishName").type(JsonFieldType.STRING).description("영어 이름"),
fieldWithPath("gender").type(JsonFieldType.STRING).description("성별 (MALE / FEMALE / UNKNOWN)"),
fieldWithPath("nationality").type(JsonFieldType.STRING).description("국적").optional(),
fieldWithPath("birthYear").type(JsonFieldType.NUMBER).description("출생 연도").optional(),
fieldWithPath("deathYear").type(JsonFieldType.NUMBER).description("사망 연도").optional(),
fieldWithPath("bio").type(JsonFieldType.STRING).description("소개").optional(),
fieldWithPath("era").type(JsonFieldType.STRING).description("시대 (MEDIEVAL_RENAISSANCE / BAROQUE / CLASSICAL / ROMANTIC / MODERN_CONTEMPORARY)").optional(),
fieldWithPath("continent").type(JsonFieldType.STRING).description("대륙 (ASIA / NORTH_AMERICA / EUROPE / SOUTH_AMERICA / AFRICA_OCEANIA)").optional()
)
.requestFields(
fieldWithPath("koreanName").type(JsonFieldType.STRING).description("한국어 이름"),
fieldWithPath("englishName").type(JsonFieldType.STRING).description("영어 이름"),
fieldWithPath("nativeName").type(JsonFieldType.STRING).description("현지어 이름").optional(),
fieldWithPath("gender").type(JsonFieldType.STRING).description("성별 (MALE / FEMALE / UNKNOWN)"),
fieldWithPath("nationality").type(JsonFieldType.STRING).description("국적").optional(),
fieldWithPath("birthYear").type(JsonFieldType.NUMBER).description("출생 연도").optional(),
fieldWithPath("deathYear").type(JsonFieldType.NUMBER).description("사망 연도").optional(),
fieldWithPath("bio").type(JsonFieldType.STRING).description("소개").optional(),
fieldWithPath("era").type(JsonFieldType.STRING).description("시대 (MEDIEVAL_RENAISSANCE / BAROQUE / CLASSICAL / ROMANTIC / MODERN_CONTEMPORARY)").optional(),
fieldWithPath("continent").type(JsonFieldType.STRING).description("대륙 (ASIA / NORTH_AMERICA / EUROPE / SOUTH_AMERICA / AFRICA_OCEANIA)").optional()
)

.build()
),
requestCookies(
cookieWithName(COOKIE_NAME).description("유저의 토큰")
)
));
}

@Test
void 작곡가_좋아요를_토글한다() throws Exception {
// given
Expand Down
Loading