1
1
package com.j.docs.student.controller
2
2
3
3
import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document
4
- import com.epages.restdocs.apispec.ResourceDocumentation
5
- import com.epages.restdocs.apispec.ResourceSnippetParameters
6
4
import com.j.docs.common.createRequest
7
5
import com.j.docs.common.getDocumentRequest
8
6
import com.j.docs.common.getDocumentResponse
9
7
import com.j.docs.common.response.CommonResponse
10
8
import com.j.docs.common.toObject
11
9
import com.j.docs.student.controller.response.StudentAllSearchResponse
10
+ import com.j.docs.student.controller.response.StudentSearchResponse
12
11
import com.j.docs.student.dto.StudentSearchDto
13
12
import com.j.docs.student.service.StudentCreationService
14
13
import com.j.docs.student.service.StudentSearchService
14
+ import org.assertj.core.api.Assertions.assertThat
15
15
import org.junit.jupiter.api.Test
16
- import org.mockito.BDDMockito.given
16
+ import org.mockito.BDDMockito.*
17
17
import org.springframework.beans.factory.annotation.Autowired
18
18
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
19
19
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
@@ -41,8 +41,8 @@ internal class StudentControllerTest {
41
41
private lateinit var studentSearchService: StudentSearchService
42
42
43
43
private val savedStudents = listOf (
44
- StudentSearchDto (1 , " 진혁 이 " , " 3417" ),
45
- StudentSearchDto (2 , " 진혁 리 " , " 3517" ),
44
+ StudentSearchDto (1 , " jinhyeok lee " , " 3417" ),
45
+ StudentSearchDto (2 , " jinhyuk lee " , " 3517" ),
46
46
)
47
47
48
48
@Test
@@ -58,7 +58,7 @@ internal class StudentControllerTest {
58
58
.andExpect(status().isOk)
59
59
.andDo(
60
60
document(
61
- " testA " ,
61
+ " 모든 학생 조회하기 - OK " ,
62
62
getDocumentRequest(),
63
63
getDocumentResponse(),
64
64
responseFields(
@@ -81,105 +81,79 @@ internal class StudentControllerTest {
81
81
.type(JsonFieldType .NULL )
82
82
.description(" 에러 메시지" ),
83
83
),
84
- )
84
+ ),
85
85
)
86
86
.andReturn()
87
87
.response
88
88
.contentAsString
89
89
.toObject<CommonResponse <StudentAllSearchResponse >>()
90
90
91
- println (" result: $result " )
91
+ verify(studentSearchService).searchAll()
92
+
93
+ assertThat(result.errorCode).isNull()
94
+ assertThat(result.errorMessage).isNull()
95
+ assertThat(result.response).isNotNull
96
+ assertThat(result.response!! .students)
97
+ .map<Long > { it.id }
98
+ .contains(1 , 2 )
99
+ assertThat(result.response!! .students)
100
+ .map<String > { it.name }
101
+ .contains(" jinhyeok lee" , " jinhyuk lee" )
102
+ assertThat(result.response!! .students)
103
+ .map<String > { it.gradeClassNumber }
104
+ .contains(" 3417" , " 3517" )
92
105
}
93
106
94
- // @Test
95
- // fun `학생 등록하기 - CREATED`() {
96
- // willDoNothing().given(
97
- // studentCreationService.create(
98
- // studentFirstName = "진혁",
99
- // studentLastName = "이",
100
- // studentGrade = 3,
101
- // studentClassroom = 4,
102
- // studentNumber = 17,
103
- // )
104
- // )
105
- //
106
- // val requestBody = StudentCreationRequest(
107
- // student = StudentCreationRequest.StudentInfo(
108
- // firstName = "진혁",
109
- // lastName = "이",
110
- // grade = 3,
111
- // classroom = 4,
112
- // number = 17,
113
- // )
114
- // )
115
- //
116
- // val result = mockMvc.perform(
117
- // createRequest(
118
- // httpMethod = HttpMethod.POST,
119
- // url = "/students",
120
- // requestBody = requestBody,
121
- // ))
122
- // .andExpect(status().isCreated)
123
- // .andDo(
124
- // document(
125
- // "학생 등록하기 - CREATED",
126
- // "",
127
- // false,
128
- // ResourceDocumentation.resource(
129
- // ResourceSnippetParameters.builder()
130
- // .requestFields(
131
- // fieldWithPath("student.firstName")
132
- // .type(SimpleType.STRING)
133
- // .description("학생의 이름(first name)"),
134
- // fieldWithPath("student.lastName")
135
- // .type(SimpleType.STRING)
136
- // .description("학생의 성(last name)"),
137
- // fieldWithPath("student.grade")
138
- // .type(SimpleType.INTEGER)
139
- // .description("학생의 학년"),
140
- // fieldWithPath("student.classroom")
141
- // .type(SimpleType.INTEGER)
142
- // .description("학생의 반"),
143
- // fieldWithPath("student.number")
144
- // .type(SimpleType.INTEGER)
145
- // .description("학생의 번호"),
146
- // ).responseFields(
147
- // fieldWithPath("response")
148
- // .type(JsonFieldType.NULL)
149
- // .description("응답 데이터 없음"),
150
- // fieldWithPath("errorCode")
151
- // .type(SimpleType.STRING)
152
- // .description("에러 코드"),
153
- // fieldWithPath("errorMessage")
154
- // .type(SimpleType.STRING)
155
- // .description("에러 메시지"),
156
- // ).build()
157
- // )
158
- // )
159
- // )
160
- // .andReturn()
161
- // .response
162
- // .contentAsString
163
- // .toObject<CommonResponse<Nothing>>()
164
- //
165
- // // verify(studentCreationService).create(
166
- // // studentFirstName = "진혁",
167
- // // studentLastName = "이",
168
- // // studentGrade = 3,
169
- // // studentClassroom = 4,
170
- // // studentNumber = 17,
171
- // // )
172
- // then(studentCreationService)
173
- // .should()
174
- // .create(
175
- // studentFirstName = "진혁",
176
- // studentLastName = "이",
177
- // studentGrade = 3,
178
- // studentClassroom = 4,
179
- // studentNumber = 17,
180
- // )
181
- //
182
- // assertThat(result.errorCode).isNull()
183
- // assertThat(result.errorMessage).isNull()
184
- // }
107
+ @Test
108
+ fun `특정 학생 조회하기 - OK` () {
109
+ willDoNothing()
110
+ .given(studentSearchService)
111
+ .search(anyLong())
112
+
113
+ val result = mockMvc.perform(
114
+ createRequest<Nothing >(
115
+ httpMethod = HttpMethod .GET ,
116
+ url = " /students/{studentId}" ,
117
+ pathVariables = listOf (1 ),
118
+ ))
119
+ .andExpect(status().isOk)
120
+ .andDo(
121
+ document(
122
+ " 특정 학생 조회하기 - OK" ,
123
+ getDocumentRequest(),
124
+ getDocumentResponse(),
125
+ responseFields(
126
+ fieldWithPath(" response.student.id" )
127
+ .type(JsonFieldType .ARRAY )
128
+ .description(" 학생 아이디" ),
129
+ fieldWithPath(" response.student.name" )
130
+ .type(JsonFieldType .ARRAY )
131
+ .description(" 학생 이름" ),
132
+ fieldWithPath(" response.student.gradeClassNumber" )
133
+ .type(JsonFieldType .ARRAY )
134
+ .description(" 학생 학년-반-번호" ),
135
+ fieldWithPath(" errorCode" )
136
+ .type(JsonFieldType .NULL )
137
+ .description(" 에러 코드" ),
138
+ fieldWithPath(" errorMessage" )
139
+ .type(JsonFieldType .NULL )
140
+ .description(" 에러 메시지" ),
141
+ ),
142
+ ),
143
+ )
144
+ .andReturn()
145
+ .response
146
+ .contentAsString
147
+ .toObject<CommonResponse <StudentSearchResponse >>()
148
+
149
+ verify(studentSearchService).search(anyLong())
150
+
151
+ assertThat(result.errorCode).isNull()
152
+ assertThat(result.errorMessage).isNull()
153
+ assertThat(result.response).isNotNull
154
+ assertThat(result.response!! .student).isNotNull
155
+ assertThat(result.response!! .student.id).isEqualTo(1 )
156
+ assertThat(result.response!! .student.name).isEqualTo(" jinhyeok lee" )
157
+ assertThat(result.response!! .student.gradeClassNumber).isEqualTo(" 3417" )
158
+ }
185
159
}
0 commit comments