@@ -3,16 +3,17 @@ package com.j.docs.student.controller
3
3
import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document
4
4
import com.epages.restdocs.apispec.ResourceDocumentation
5
5
import com.epages.restdocs.apispec.ResourceSnippetParameters
6
- import com.epages.restdocs.apispec.SimpleType
7
6
import com.j.docs.common.createRequest
7
+ import com.j.docs.common.getDocumentRequest
8
+ import com.j.docs.common.getDocumentResponse
8
9
import com.j.docs.common.response.CommonResponse
9
10
import com.j.docs.common.toObject
10
- import com.j.docs.student.controller.request.StudentCreationRequest
11
+ import com.j.docs.student.controller.response.StudentAllSearchResponse
12
+ import com.j.docs.student.dto.StudentSearchDto
11
13
import com.j.docs.student.service.StudentCreationService
12
14
import com.j.docs.student.service.StudentSearchService
13
- import org.assertj.core.api.Assertions.assertThat
14
15
import org.junit.jupiter.api.Test
15
- import org.mockito.BDDMockito.*
16
+ import org.mockito.BDDMockito.given
16
17
import org.springframework.beans.factory.annotation.Autowired
17
18
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
18
19
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
@@ -21,6 +22,7 @@ import org.springframework.boot.test.mock.mockito.MockBean
21
22
import org.springframework.http.HttpMethod
22
23
import org.springframework.restdocs.payload.JsonFieldType
23
24
import org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath
25
+ import org.springframework.restdocs.payload.PayloadDocumentation.responseFields
24
26
import org.springframework.test.web.servlet.MockMvc
25
27
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
26
28
@@ -38,95 +40,146 @@ internal class StudentControllerTest {
38
40
@MockBean
39
41
private lateinit var studentSearchService: StudentSearchService
40
42
41
- @Test
42
- fun `학생 등록하기 - CREATED` () {
43
- willDoNothing().given(
44
- studentCreationService.create(
45
- studentFirstName = " 진혁" ,
46
- studentLastName = " 이" ,
47
- studentGrade = 3 ,
48
- studentClassroom = 4 ,
49
- studentNumber = 17 ,
50
- )
51
- )
43
+ private val savedStudents = listOf (
44
+ StudentSearchDto (1 , " 진혁 이" , " 3417" ),
45
+ StudentSearchDto (2 , " 진혁 리" , " 3517" ),
46
+ )
52
47
53
- val requestBody = StudentCreationRequest (
54
- student = StudentCreationRequest .StudentInfo (
55
- firstName = " 진혁" ,
56
- lastName = " 이" ,
57
- grade = 3 ,
58
- classroom = 4 ,
59
- number = 17 ,
60
- )
61
- )
48
+ @Test
49
+ fun `모든 학생 조회하기 - OK` () {
50
+ given(studentSearchService.searchAll())
51
+ .willReturn(savedStudents)
62
52
63
53
val result = mockMvc.perform(
64
- createRequest(
65
- httpMethod = HttpMethod .POST ,
54
+ createRequest< Nothing > (
55
+ httpMethod = HttpMethod .GET ,
66
56
url = " /students" ,
67
- requestBody = requestBody,
68
57
))
69
- .andExpect(status().isCreated )
58
+ .andExpect(status().isOk )
70
59
.andDo(
71
60
document(
72
- " 학생 등록하기 - CREATED" ,
73
- " " ,
74
- false ,
75
- ResourceDocumentation .resource(
76
- ResourceSnippetParameters .builder()
77
- .requestFields(
78
- fieldWithPath(" student.firstName" )
79
- .type(SimpleType .STRING )
80
- .description(" 학생의 이름(first name)" ),
81
- fieldWithPath(" student.lastName" )
82
- .type(SimpleType .STRING )
83
- .description(" 학생의 성(last name)" ),
84
- fieldWithPath(" student.grade" )
85
- .type(SimpleType .INTEGER )
86
- .description(" 학생의 학년" ),
87
- fieldWithPath(" student.classroom" )
88
- .type(SimpleType .INTEGER )
89
- .description(" 학생의 반" ),
90
- fieldWithPath(" student.number" )
91
- .type(SimpleType .INTEGER )
92
- .description(" 학생의 번호" ),
93
- ).responseFields(
94
- fieldWithPath(" response" )
95
- .type(JsonFieldType .NULL )
96
- .description(" 응답 데이터 없음" ),
97
- fieldWithPath(" errorCode" )
98
- .type(SimpleType .STRING )
99
- .description(" 에러 코드" ),
100
- fieldWithPath(" errorMessage" )
101
- .type(SimpleType .STRING )
102
- .description(" 에러 메시지" ),
103
- ).build()
104
- )
61
+ " testA" ,
62
+ getDocumentRequest(),
63
+ getDocumentResponse(),
64
+ responseFields(
65
+ fieldWithPath(" response.students" )
66
+ .type(JsonFieldType .ARRAY )
67
+ .description(" 조회한 학생들" ),
68
+ fieldWithPath(" response.students[].id" )
69
+ .type(JsonFieldType .NUMBER )
70
+ .description(" 학생 아이디" ),
71
+ fieldWithPath(" response.students[].name" )
72
+ .type(JsonFieldType .STRING )
73
+ .description(" 학생 이름" ),
74
+ fieldWithPath(" response.students[].gradeClassNumber" )
75
+ .type(JsonFieldType .STRING )
76
+ .description(" 학생 학년-반-번호" ),
77
+ fieldWithPath(" errorCode" )
78
+ .type(JsonFieldType .NULL )
79
+ .description(" 에러 코드" ),
80
+ fieldWithPath(" errorMessage" )
81
+ .type(JsonFieldType .NULL )
82
+ .description(" 에러 메시지" ),
83
+ ),
105
84
)
106
85
)
107
86
.andReturn()
108
87
.response
109
88
.contentAsString
110
- .toObject<CommonResponse <Nothing >>()
111
-
112
- // verify(studentCreationService).create(
113
- // studentFirstName = "진혁",
114
- // studentLastName = "이",
115
- // studentGrade = 3,
116
- // studentClassroom = 4,
117
- // studentNumber = 17,
118
- // )
119
- then(studentCreationService)
120
- .should()
121
- .create(
122
- studentFirstName = " 진혁" ,
123
- studentLastName = " 이" ,
124
- studentGrade = 3 ,
125
- studentClassroom = 4 ,
126
- studentNumber = 17 ,
127
- )
89
+ .toObject<CommonResponse <StudentAllSearchResponse >>()
128
90
129
- assertThat(result.errorCode).isNull()
130
- assertThat(result.errorMessage).isNull()
91
+ println (" result: $result " )
131
92
}
93
+
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
+ // }
132
185
}
0 commit comments