1
1
package com.j.docs.student.controller
2
2
3
3
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.*
7
5
import com.j.docs.common.response.CommonResponse
8
- import com.j.docs.common.toObject
6
+ import com.j.docs.student.controller.request.StudentCreationRequest
9
7
import com.j.docs.student.controller.response.StudentAllSearchResponse
10
8
import com.j.docs.student.controller.response.StudentSearchResponse
11
9
import com.j.docs.student.dto.StudentSearchDto
@@ -21,8 +19,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
21
19
import org.springframework.boot.test.mock.mockito.MockBean
22
20
import org.springframework.http.HttpMethod
23
21
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.*
26
23
import org.springframework.test.web.servlet.MockMvc
27
24
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
28
25
@@ -40,15 +37,23 @@ internal class StudentControllerTest {
40
37
@MockBean
41
38
private lateinit var studentSearchService: StudentSearchService
42
39
43
- private val savedStudents = listOf (
44
- StudentSearchDto (1 , " jinhyeok lee" , " 3417" ),
45
- StudentSearchDto (2 , " jinhyuk lee" , " 3517" ),
46
- )
47
-
48
40
@Test
49
41
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
+
50
55
given(studentSearchService.searchAll())
51
- .willReturn(savedStudents )
56
+ .willReturn(returnValue )
52
57
53
58
val result = mockMvc.perform(
54
59
createRequest<Nothing >(
@@ -106,15 +111,21 @@ internal class StudentControllerTest {
106
111
107
112
@Test
108
113
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)
112
123
113
124
val result = mockMvc.perform(
114
125
createRequest<Nothing >(
115
126
httpMethod = HttpMethod .GET ,
116
127
url = " /students/{studentId}" ,
117
- pathVariables = listOf (1 ),
128
+ pathVariables = listOf (" studentId " to 1 ),
118
129
))
119
130
.andExpect(status().isOk)
120
131
.andDo(
@@ -124,13 +135,13 @@ internal class StudentControllerTest {
124
135
getDocumentResponse(),
125
136
responseFields(
126
137
fieldWithPath(" response.student.id" )
127
- .type(JsonFieldType .ARRAY )
138
+ .type(JsonFieldType .NUMBER )
128
139
.description(" 학생 아이디" ),
129
140
fieldWithPath(" response.student.name" )
130
- .type(JsonFieldType .ARRAY )
141
+ .type(JsonFieldType .STRING )
131
142
.description(" 학생 이름" ),
132
143
fieldWithPath(" response.student.gradeClassNumber" )
133
- .type(JsonFieldType .ARRAY )
144
+ .type(JsonFieldType .STRING )
134
145
.description(" 학생 학년-반-번호" ),
135
146
fieldWithPath(" errorCode" )
136
147
.type(JsonFieldType .NULL )
@@ -146,7 +157,9 @@ internal class StudentControllerTest {
146
157
.contentAsString
147
158
.toObject<CommonResponse <StudentSearchResponse >>()
148
159
149
- verify(studentSearchService).search(anyLong())
160
+ verify(studentSearchService).search(
161
+ studentId = anyLong(),
162
+ )
150
163
151
164
assertThat(result.errorCode).isNull()
152
165
assertThat(result.errorMessage).isNull()
@@ -156,4 +169,87 @@ internal class StudentControllerTest {
156
169
assertThat(result.response!! .student.name).isEqualTo(" jinhyeok lee" )
157
170
assertThat(result.response!! .student.gradeClassNumber).isEqualTo(" 3417" )
158
171
}
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
+ }
159
255
}
0 commit comments