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 8e14a44

Browse files
test: 모든 학생 조회하기 테스트 코드 추가
1 parent 215545a commit 8e14a44

File tree

8 files changed

+118
-103
lines changed

8 files changed

+118
-103
lines changed

‎.idea/codeStyles/Project.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/kotlinc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/runConfigurations.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.j.docs.common
22

3+
import org.springframework.restdocs.operation.preprocess.OperationRequestPreprocessor
4+
import org.springframework.restdocs.operation.preprocess.OperationResponsePreprocessor
35
import org.springframework.restdocs.operation.preprocess.Preprocessors.*
46

5-
fun getDocumentRequest() =
7+
fun getDocumentRequest(): OperationRequestPreprocessor =
68
preprocessRequest(
79
modifyUris()
810
.scheme("http")
@@ -11,4 +13,5 @@ fun getDocumentRequest() =
1113
prettyPrint(),
1214
)
1315

14-
fun getDocumentResponse() = preprocessResponse(prettyPrint())
16+
fun getDocumentResponse(): OperationResponsePreprocessor =
17+
preprocessResponse(prettyPrint())

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ fun <T> createRequest(
1515
httpMethod: HttpMethod,
1616
url: String,
1717
requestBody: T? = null,
18-
vararg queryParams: Pair<String, Any>,
18+
queryParams: List<Pair<String, Any>> = listOf(),
19+
pathVariables: List<Any> = listOf(),
1920
): MockHttpServletRequestBuilder =
20-
RestDocumentationRequestBuilders.request(httpMethod, url)
21+
RestDocumentationRequestBuilders.request(httpMethod, url, pathVariables)
2122
.accept(MediaType.APPLICATION_JSON)
2223
.contentType(MediaType.APPLICATION_JSON)
2324
.characterEncoding("UTF-8")
Lines changed: 73 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package com.j.docs.student.controller
22

33
import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document
4-
import com.epages.restdocs.apispec.ResourceDocumentation
5-
import com.epages.restdocs.apispec.ResourceSnippetParameters
64
import com.j.docs.common.createRequest
75
import com.j.docs.common.getDocumentRequest
86
import com.j.docs.common.getDocumentResponse
97
import com.j.docs.common.response.CommonResponse
108
import com.j.docs.common.toObject
119
import com.j.docs.student.controller.response.StudentAllSearchResponse
10+
import com.j.docs.student.controller.response.StudentSearchResponse
1211
import com.j.docs.student.dto.StudentSearchDto
1312
import com.j.docs.student.service.StudentCreationService
1413
import com.j.docs.student.service.StudentSearchService
14+
import org.assertj.core.api.Assertions.assertThat
1515
import org.junit.jupiter.api.Test
16-
import org.mockito.BDDMockito.given
16+
import org.mockito.BDDMockito.*
1717
import org.springframework.beans.factory.annotation.Autowired
1818
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
1919
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
@@ -41,8 +41,8 @@ internal class StudentControllerTest {
4141
private lateinit var studentSearchService: StudentSearchService
4242

4343
private val savedStudents = listOf(
44-
StudentSearchDto(1, "진혁 이", "3417"),
45-
StudentSearchDto(2, "진혁 리", "3517"),
44+
StudentSearchDto(1, "jinhyeok lee", "3417"),
45+
StudentSearchDto(2, "jinhyuk lee", "3517"),
4646
)
4747

4848
@Test
@@ -58,7 +58,7 @@ internal class StudentControllerTest {
5858
.andExpect(status().isOk)
5959
.andDo(
6060
document(
61-
"testA",
61+
"모든 학생 조회하기 - OK",
6262
getDocumentRequest(),
6363
getDocumentResponse(),
6464
responseFields(
@@ -81,105 +81,79 @@ internal class StudentControllerTest {
8181
.type(JsonFieldType.NULL)
8282
.description("에러 메시지"),
8383
),
84-
)
84+
),
8585
)
8686
.andReturn()
8787
.response
8888
.contentAsString
8989
.toObject<CommonResponse<StudentAllSearchResponse>>()
9090

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")
92105
}
93106

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+
}
185159
}

0 commit comments

Comments
(0)

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