Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d6f661a

Browse files
test: 학생 등록 API 테스트 코드 추가
1 parent 8e14a44 commit d6f661a

File tree

2 files changed

+134
-23
lines changed

2 files changed

+134
-23
lines changed

‎src/test/kotlin/com/j/docs/common/TestRequests.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ fun <T> createRequest(
1616
url: String,
1717
requestBody: T? = null,
1818
queryParams: List<Pair<String, Any>> = listOf(),
19-
pathVariables: List<Any> = listOf(),
19+
pathVariables: List<Pair<String, Any>> = listOf(),
2020
): MockHttpServletRequestBuilder =
21-
RestDocumentationRequestBuilders.request(httpMethod, url, pathVariables)
21+
RestDocumentationRequestBuilders.request(
22+
httpMethod,
23+
applyPathVariable(url, pathVariables),
24+
pathVariables)
2225
.accept(MediaType.APPLICATION_JSON)
2326
.contentType(MediaType.APPLICATION_JSON)
2427
.characterEncoding("UTF-8")
@@ -29,5 +32,17 @@ fun <T> createRequest(
2932
}
3033
})
3134

35+
private fun applyPathVariable(url: String, pathVariables: List<Pair<String, Any>>): String {
36+
var urlWithPathVariableApplied = url
37+
pathVariables.forEach { (path, pathVariable) ->
38+
urlWithPathVariableApplied =
39+
urlWithPathVariableApplied.replace("{$path}", pathVariable.toString())
40+
}
41+
return urlWithPathVariableApplied
42+
}
43+
3244
inline fun <reified T> String.toObject() =
33-
ObjectMapper().findAndRegisterModules().readValue<T>(this)
45+
ObjectMapper().findAndRegisterModules().readValue<T>(this)
46+
47+
fun Any.toJson() =
48+
ObjectMapper().writeValueAsString(this)

‎src/test/kotlin/com/j/docs/student/controller/StudentControllerTest.kt

Lines changed: 116 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.j.docs.student.controller
22

33
import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document
4-
import com.j.docs.common.createRequest
5-
import com.j.docs.common.getDocumentRequest
6-
import com.j.docs.common.getDocumentResponse
4+
import com.j.docs.common.*
75
import com.j.docs.common.response.CommonResponse
8-
import com.j.docs.common.toObject
6+
import com.j.docs.student.controller.request.StudentCreationRequest
97
import com.j.docs.student.controller.response.StudentAllSearchResponse
108
import com.j.docs.student.controller.response.StudentSearchResponse
119
import com.j.docs.student.dto.StudentSearchDto
@@ -21,8 +19,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
2119
import org.springframework.boot.test.mock.mockito.MockBean
2220
import org.springframework.http.HttpMethod
2321
import org.springframework.restdocs.payload.JsonFieldType
24-
import org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath
25-
import org.springframework.restdocs.payload.PayloadDocumentation.responseFields
22+
import org.springframework.restdocs.payload.PayloadDocumentation.*
2623
import org.springframework.test.web.servlet.MockMvc
2724
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
2825

@@ -40,15 +37,23 @@ internal class StudentControllerTest {
4037
@MockBean
4138
private lateinit var studentSearchService: StudentSearchService
4239

43-
private val savedStudents = listOf(
44-
StudentSearchDto(1, "jinhyeok lee", "3417"),
45-
StudentSearchDto(2, "jinhyuk lee", "3517"),
46-
)
47-
4840
@Test
4941
fun `모든 학생 조회하기 - OK`() {
42+
val returnValue = listOf(
43+
StudentSearchDto(
44+
studentId = 1,
45+
studentName = "jinhyeok lee",
46+
studentGradeClassNumber = "3417",
47+
),
48+
StudentSearchDto(
49+
studentId = 2,
50+
studentName = "jinhyuk lee",
51+
studentGradeClassNumber = "3517",
52+
),
53+
)
54+
5055
given(studentSearchService.searchAll())
51-
.willReturn(savedStudents)
56+
.willReturn(returnValue)
5257

5358
val result = mockMvc.perform(
5459
createRequest<Nothing>(
@@ -106,15 +111,21 @@ internal class StudentControllerTest {
106111

107112
@Test
108113
fun `특정 학생 조회하기 - OK`() {
109-
willDoNothing()
110-
.given(studentSearchService)
111-
.search(anyLong())
114+
val returnValue = StudentSearchDto(
115+
studentId = 1,
116+
studentName = "jinhyeok lee",
117+
studentGradeClassNumber = "3417",
118+
)
119+
120+
given(studentSearchService.search(
121+
studentId = anyLong(),
122+
)).willReturn(returnValue)
112123

113124
val result = mockMvc.perform(
114125
createRequest<Nothing>(
115126
httpMethod = HttpMethod.GET,
116127
url = "/students/{studentId}",
117-
pathVariables = listOf(1),
128+
pathVariables = listOf("studentId" to 1),
118129
))
119130
.andExpect(status().isOk)
120131
.andDo(
@@ -124,13 +135,13 @@ internal class StudentControllerTest {
124135
getDocumentResponse(),
125136
responseFields(
126137
fieldWithPath("response.student.id")
127-
.type(JsonFieldType.ARRAY)
138+
.type(JsonFieldType.NUMBER)
128139
.description("학생 아이디"),
129140
fieldWithPath("response.student.name")
130-
.type(JsonFieldType.ARRAY)
141+
.type(JsonFieldType.STRING)
131142
.description("학생 이름"),
132143
fieldWithPath("response.student.gradeClassNumber")
133-
.type(JsonFieldType.ARRAY)
144+
.type(JsonFieldType.STRING)
134145
.description("학생 학년-반-번호"),
135146
fieldWithPath("errorCode")
136147
.type(JsonFieldType.NULL)
@@ -146,7 +157,9 @@ internal class StudentControllerTest {
146157
.contentAsString
147158
.toObject<CommonResponse<StudentSearchResponse>>()
148159

149-
verify(studentSearchService).search(anyLong())
160+
verify(studentSearchService).search(
161+
studentId = anyLong(),
162+
)
150163

151164
assertThat(result.errorCode).isNull()
152165
assertThat(result.errorMessage).isNull()
@@ -156,4 +169,87 @@ internal class StudentControllerTest {
156169
assertThat(result.response!!.student.name).isEqualTo("jinhyeok lee")
157170
assertThat(result.response!!.student.gradeClassNumber).isEqualTo("3417")
158171
}
172+
173+
@Test
174+
fun `학생 등록하기 - Created`() {
175+
willDoNothing()
176+
.given(studentCreationService)
177+
.create(
178+
studentFirstName = anyString(),
179+
studentLastName = anyString(),
180+
studentGrade = anyInt(),
181+
studentClassroom = anyInt(),
182+
studentNumber = anyInt(),
183+
)
184+
185+
val request = StudentCreationRequest(
186+
student = StudentCreationRequest.StudentInfo(
187+
firstName = "jinhyeok",
188+
lastName = "lee",
189+
grade = 3,
190+
classroom = 4,
191+
number = 17,
192+
)
193+
)
194+
195+
val result = mockMvc.perform(
196+
createRequest(
197+
httpMethod = HttpMethod.POST,
198+
url = "/students",
199+
requestBody = request,
200+
))
201+
.andExpect(status().isCreated)
202+
.andDo(
203+
document(
204+
"학생 등록하기 - Created",
205+
getDocumentRequest(),
206+
getDocumentResponse(),
207+
requestFields(
208+
fieldWithPath("student.firstName")
209+
.type(JsonFieldType.STRING)
210+
.description("학생 이름"),
211+
fieldWithPath("student.lastName")
212+
.type(JsonFieldType.STRING)
213+
.description("학생 성"),
214+
fieldWithPath("student.grade")
215+
.type(JsonFieldType.NUMBER)
216+
.description("학생 학년"),
217+
fieldWithPath("student.classroom")
218+
.type(JsonFieldType.NUMBER)
219+
.description("학생 반"),
220+
fieldWithPath("student.number")
221+
.type(JsonFieldType.NUMBER)
222+
.description("학생 번호"),
223+
),
224+
responseFields(
225+
fieldWithPath("response")
226+
.type(JsonFieldType.NULL)
227+
.description("응답 내용"),
228+
fieldWithPath("errorCode")
229+
.type(JsonFieldType.NULL)
230+
.description("에러 코드"),
231+
fieldWithPath("errorMessage")
232+
.type(JsonFieldType.NULL)
233+
.description("에러 메시지"),
234+
),
235+
),
236+
)
237+
.andReturn()
238+
.response
239+
.contentAsString
240+
.toObject<CommonResponse<StudentSearchResponse>>()
241+
242+
verify(studentCreationService)
243+
.create(
244+
studentFirstName = anyString(),
245+
studentLastName = anyString(),
246+
studentGrade = anyInt(),
247+
studentClassroom = anyInt(),
248+
studentNumber = anyInt(),
249+
)
250+
251+
assertThat(result.response).isNull()
252+
assertThat(result.errorCode).isNull()
253+
assertThat(result.errorMessage).isNull()
254+
}
159255
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /