Skip to content

Commit 7e79193

Browse files
authored
테스트 오류를 해결한다. (#49)
fix: fix test
1 parent 12105f2 commit 7e79193

File tree

11 files changed

+65
-68
lines changed

11 files changed

+65
-68
lines changed

api/src/main/kotlin/com/gotchai/api/presentation/v1/badge/BadgeController.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.gotchai.api.presentation.v1.badge
22

33
import com.gotchai.api.global.annotation.ApiV1Controller
44
import com.gotchai.api.presentation.v1.badge.response.BadgeResponse
5-
import com.gotchai.api.presentation.v1.badge.response.GetMyBadgeResponse
5+
import com.gotchai.api.presentation.v1.badge.response.GetMyBadgeListResponse
66
import com.gotchai.domain.badge.port.`in`.BadgeQueryUseCase
77
import org.springframework.security.core.annotation.AuthenticationPrincipal
88
import org.springframework.web.bind.annotation.GetMapping
@@ -25,8 +25,8 @@ class BadgeController(
2525
fun getMyBadges(
2626
@AuthenticationPrincipal
2727
userId: Long
28-
): List<GetMyBadgeResponse> =
28+
): GetMyBadgeListResponse =
2929
badgeQueryUseCase
3030
.getMyBadges(userId)
31-
.map { GetMyBadgeResponse.from(it) }
31+
.let { GetMyBadgeListResponse.from(it) }
3232
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.gotchai.api.presentation.v1.badge.response
2+
3+
import com.gotchai.domain.badge.dto.result.GetMyBadgeResult
4+
5+
data class GetMyBadgeListResponse(
6+
val list: List<GetMyBadgeResponse>
7+
) {
8+
companion object {
9+
fun from(results: List<GetMyBadgeResult>): GetMyBadgeListResponse =
10+
GetMyBadgeListResponse(list = results.map { GetMyBadgeResponse.from(it) })
11+
}
12+
}

api/src/test/kotlin/com/gotchai/api/presentation/v1/badge/BadgeControllerTest.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package com.gotchai.api.presentation.v1.badge
33
import com.gotchai.api.common.ControllerTest
44
import com.gotchai.api.docs.badgeResponseFields
55
import com.gotchai.api.docs.errorResponseFields
6-
import com.gotchai.api.docs.getMyBadgeResponseFields
6+
import com.gotchai.api.docs.getMyBadgeListResponseFields
77
import com.gotchai.api.global.dto.ApiResponse
88
import com.gotchai.api.presentation.v1.badge.response.BadgeResponse
9-
import com.gotchai.api.presentation.v1.badge.response.GetMyBadgeResponse
9+
import com.gotchai.api.presentation.v1.badge.response.GetMyBadgeListResponse
1010
import com.gotchai.api.util.document
1111
import com.gotchai.api.util.expectError
1212
import com.gotchai.api.util.paramDesc
13-
import com.gotchai.api.util.toListFields
1413
import com.gotchai.domain.badge.exception.BadgeNotFoundException
1514
import com.gotchai.domain.badge.port.`in`.BadgeQueryUseCase
1615
import com.gotchai.domain.fixture.ID
@@ -84,9 +83,9 @@ class BadgeControllerTest : ControllerTest() {
8483
.exchange()
8584
.expectStatus()
8685
.isOk
87-
.expectBody<ApiResponse<List<GetMyBadgeResponse>>>()
86+
.expectBody<ApiResponse<GetMyBadgeListResponse>>()
8887
.document("내가 취득한 뱃지 리스트 조회 성공(200)") {
89-
responseBody(getMyBadgeResponseFields.toListFields())
88+
responseBody(getMyBadgeListResponseFields)
9089
}
9190
}
9291
}

api/src/test/kotlin/com/gotchai/api/presentation/v1/exam/ExamControllerTest.kt

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package com.gotchai.api.presentation.v1.exam
33
import com.gotchai.api.common.ControllerTest
44
import com.gotchai.api.docs.errorResponseFields
55
import com.gotchai.api.docs.examDetailResponseFields
6-
import com.gotchai.api.docs.examResponseFields
6+
import com.gotchai.api.docs.examListResponseFields
77
import com.gotchai.api.global.dto.ApiResponse
88
import com.gotchai.api.presentation.v1.exam.response.ExamDetailResponse
9-
import com.gotchai.api.presentation.v1.exam.response.ExamResponse
9+
import com.gotchai.api.presentation.v1.exam.response.ExamListResponse
1010
import com.gotchai.api.util.document
1111
import com.gotchai.api.util.expectError
1212
import com.gotchai.api.util.paramDesc
13-
import com.gotchai.api.util.toListFields
1413
import com.gotchai.domain.exam.port.`in`.ExamQueryUseCase
1514
import com.gotchai.domain.fixture.ID
1615
import com.gotchai.domain.fixture.createExam
@@ -28,7 +27,7 @@ class ExamControllerTest : ControllerTest() {
2827

2928
init {
3029
describe("getExams()는") {
31-
context("시험 목록이 존재하는 경우") {
30+
context("테스트 목록이 존재하는 경우") {
3231
val exams =
3332
listOf(
3433
createExam(id = 1L, title = "AI와 크리스마스 파티"),
@@ -37,39 +36,25 @@ class ExamControllerTest : ControllerTest() {
3736

3837
every { examQueryUseCase.getExams() } returns exams
3938

40-
it("상태 코드 200과 ExamResponse 리스트를 반환한다.") {
39+
it("상태 코드 200과 ExamListResponse를 반환한다.") {
4140
webClient
4241
.get()
4342
.uri("/api/v1/exams")
4443
.exchange()
4544
.expectStatus()
4645
.isOk
47-
.expectBody<ApiResponse<List<ExamResponse>>>()
46+
.expectBody<ApiResponse<ExamListResponse>>()
4847
.document("시험 목록 조회 성공(200)") {
49-
responseBody(examResponseFields.toListFields())
48+
responseBody(examListResponseFields)
5049
}
5150
}
5251
}
53-
54-
context("시험 목록이 비어있는 경우") {
55-
every { examQueryUseCase.getExams() } returns emptyList()
56-
57-
it("상태 코드 200과 빈 리스트를 반환한다.") {
58-
webClient
59-
.get()
60-
.uri("/api/v1/exams")
61-
.exchange()
62-
.expectStatus()
63-
.isOk
64-
.expectBody<ApiResponse<List<ExamResponse>>>()
65-
}
66-
}
6752
}
6853

6954
describe("getExamById()는") {
70-
context("조회하려는 시험이 존재하는 경우") {
71-
val examResult = createGetExamResult()
72-
every { examQueryUseCase.getExamById(ID) } returns examResult
55+
context("조회하려는 테스트가 존재하는 경우") {
56+
val result = createGetExamResult()
57+
every { examQueryUseCase.getExamById(ID) } returns result
7358

7459
it("상태 코드 200과 ExamDetailResponse를 반환한다.") {
7560
webClient
@@ -86,7 +71,7 @@ class ExamControllerTest : ControllerTest() {
8671
}
8772
}
8873

89-
context("조회하려는 시험이 존재하지 않는 경우") {
74+
context("조회하려는 테스트가 존재하지 않는 경우") {
9075
every { examQueryUseCase.getExamById(any()) } throws NotFoundDataException()
9176

9277
it("상태 코드 404와 ErrorResponse를 반환한다.") {

api/src/testFixtures/kotlin/com/gotchai/api/docs/AuthDocs.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,31 @@ import com.gotchai.api.presentation.v1.auth.request.RefreshRequest
66
import com.gotchai.api.presentation.v1.auth.response.RefreshResponse
77
import com.gotchai.api.presentation.v1.auth.response.SocialLoginResponse
88
import com.gotchai.api.util.bodyDesc
9+
import com.gotchai.api.util.fieldsOf
910

1011
val appleLoginRequestFields =
11-
listOf(
12+
fieldsOf(
1213
AppleLoginRequest::idToken bodyDesc "ID 토큰"
1314
)
1415

1516
val kakaoLoginRequestFields =
16-
listOf(
17+
fieldsOf(
1718
KakaoLoginRequest::accessToken bodyDesc "카카오 액세스 토큰"
1819
)
1920

2021
val refreshRequestFields =
21-
listOf(
22+
fieldsOf(
2223
RefreshRequest::refreshToken bodyDesc "리프레시 토큰"
2324
)
2425

2526
val refreshResponseFields =
26-
listOf(
27+
fieldsOf(
2728
RefreshResponse::accessToken bodyDesc "액세스 토큰",
2829
RefreshResponse::refreshToken bodyDesc "리프레시 토큰"
2930
)
3031

3132
val socialLoginResponseFields =
32-
listOf(
33+
fieldsOf(
3334
SocialLoginResponse::accessToken bodyDesc "액세스 토큰",
3435
SocialLoginResponse::refreshToken bodyDesc "리프레시 토큰"
3536
)

api/src/testFixtures/kotlin/com/gotchai/api/docs/BadgeDocs.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package com.gotchai.api.docs
33
import com.gotchai.api.presentation.v1.badge.response.BadgeResponse
44
import com.gotchai.api.presentation.v1.badge.response.GetMyBadgeResponse
55
import com.gotchai.api.util.bodyDesc
6+
import com.gotchai.api.util.fieldsOf
7+
import com.gotchai.api.util.listFieldsOf
68

79
val badgeResponseFields =
8-
listOf(
10+
fieldsOf(
911
BadgeResponse::id bodyDesc "식별자",
1012
BadgeResponse::examId bodyDesc "테스트 식별자",
1113
BadgeResponse::name bodyDesc "이름",
@@ -15,8 +17,9 @@ val badgeResponseFields =
1517
BadgeResponse::createdAt bodyDesc "생성 날짜"
1618
)
1719

18-
val getMyBadgeResponseFields =
19-
listOf(
20+
val getMyBadgeListResponseFields =
21+
listFieldsOf(
22+
description = "뱃지 리스트",
2023
GetMyBadgeResponse::id bodyDesc "식별자",
2124
GetMyBadgeResponse::examId bodyDesc "테스트 식별자",
2225
GetMyBadgeResponse::name bodyDesc "이름",

api/src/testFixtures/kotlin/com/gotchai/api/docs/CommonDocs.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ package com.gotchai.api.docs
33
import com.gotchai.api.global.dto.ApiResponse
44
import com.gotchai.api.global.dto.ErrorResponse
55
import com.gotchai.api.util.bodyDesc
6+
import com.gotchai.api.util.fieldsOf
67

78
val apiResponseFields =
8-
listOf(
9+
fieldsOf(
910
ApiResponse<*>::isSuccess bodyDesc "처리 성공 여부",
1011
ApiResponse<*>::status bodyDesc "상태 코드",
1112
ApiResponse<*>::timestamp bodyDesc "타임 스탬프"
1213
)
1314

1415
val errorResponseFields =
15-
listOf(
16+
fieldsOf(
1617
ErrorResponse::errorCode bodyDesc "에러 코드",
1718
ErrorResponse::message bodyDesc "에러 메세지"
1819
)

api/src/testFixtures/kotlin/com/gotchai/api/docs/ExamDocs.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package com.gotchai.api.docs
33
import com.gotchai.api.presentation.v1.exam.response.ExamDetailResponse
44
import com.gotchai.api.presentation.v1.exam.response.ExamResponse
55
import com.gotchai.api.util.bodyDesc
6+
import com.gotchai.api.util.fieldsOf
7+
import com.gotchai.api.util.listFieldsOf
68

7-
val examResponseFields =
8-
listOf(
9+
val examListResponseFields =
10+
listFieldsOf(
11+
description = "테스트 리스트",
912
ExamResponse::id bodyDesc "식별자",
1013
ExamResponse::title bodyDesc "제목",
1114
ExamResponse::subTitle bodyDesc "부제목",
@@ -16,7 +19,7 @@ val examResponseFields =
1619
)
1720

1821
val examDetailResponseFields =
19-
listOf(
22+
fieldsOf(
2023
ExamDetailResponse::id bodyDesc "식별자",
2124
ExamDetailResponse::title bodyDesc "제목",
2225
ExamDetailResponse::subTitle bodyDesc "부제목",

api/src/testFixtures/kotlin/com/gotchai/api/docs/UserDocs.kt

Lines changed: 0 additions & 3 deletions
This file was deleted.

api/src/testFixtures/kotlin/com/gotchai/api/util/RestDocsUtil.kt

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ infix fun String.paramDesc(description: String): ParameterDescriptor =
2424
parameterWithName(this)
2525
.description(description)
2626

27-
fun List<FieldDescriptor>.toListFields(): List<FieldDescriptor> =
28-
map {
29-
"[].${it.path}" bodyDesc
30-
it.description as String
31-
}
27+
fun fieldsOf(vararg field: FieldDescriptor): List<FieldDescriptor> = field.asList()
28+
29+
fun listFieldsOf(
30+
description: String,
31+
vararg field: FieldDescriptor
32+
): List<FieldDescriptor> =
33+
field.map { "list[].${it.path}" bodyDesc it.description as String } + ("list" bodyDesc description)
3234

3335
fun <T> BodySpec<T, *>.document(
3436
identifier: String,
@@ -86,18 +88,12 @@ class DocumentDsl<T>(
8688
)
8789
)
8890

89-
private fun mergeWithApiResponseFields(fields: List<FieldDescriptor>): List<FieldDescriptor> {
90-
val isArray = fields.all { it.path.startsWith("[]") }
91-
val dataField = "${ApiResponse<*>::data.name}${"[]".takeIf { isArray }.orEmpty()}" bodyDesc "응답 데이터"
92-
93-
return (
94-
apiResponseFields + dataField +
95-
fields.map {
96-
val path = ApiResponse<*>::data.name + ".".takeUnless { isArray }.orEmpty() + it.path
97-
val description = it.description as String
91+
private fun mergeWithApiResponseFields(fields: List<FieldDescriptor>): List<FieldDescriptor> =
92+
apiResponseFields +
93+
fields.map {
94+
val path = "${ApiResponse<*>::data.name}.${it.path}"
95+
val description = it.description as String
9896

99-
path bodyDesc description
100-
}
101-
)
102-
}
97+
path bodyDesc description
98+
}
10399
}

0 commit comments

Comments
 (0)