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 b65d305

Browse files
feat: 컨트롤러 테스트 코드 추가
1 parent d37c4b9 commit b65d305

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

‎build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies {
3131
testImplementation("org.springframework.restdocs:spring-restdocs-mockmvc")
3232

3333
// Mockito-Kotlin
34-
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")
34+
// testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")
3535

3636
// JUnit5...etc
3737
testImplementation("org.springframework.boot:spring-boot-starter-test")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.j.docs.student.common
1+
package com.j.docs.common
22

33
import com.fasterxml.jackson.databind.ObjectMapper
44
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper

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

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document
44
import com.epages.restdocs.apispec.ResourceDocumentation
55
import com.epages.restdocs.apispec.ResourceSnippetParameters
66
import com.epages.restdocs.apispec.SimpleType
7+
import com.j.docs.common.createRequest
78
import com.j.docs.common.response.CommonResponse
8-
import com.j.docs.student.common.createRequest
9-
import com.j.docs.student.common.toObject
9+
import com.j.docs.common.toObject
1010
import com.j.docs.student.controller.request.StudentCreationRequest
1111
import com.j.docs.student.service.StudentCreationService
1212
import com.j.docs.student.service.StudentSearchService
13+
import org.assertj.core.api.Assertions.assertThat
1314
import org.junit.jupiter.api.Test
15+
import org.mockito.BDDMockito.*
1416
import org.springframework.beans.factory.annotation.Autowired
1517
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
1618
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
@@ -20,7 +22,7 @@ import org.springframework.http.HttpMethod
2022
import org.springframework.restdocs.payload.JsonFieldType
2123
import org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath
2224
import org.springframework.test.web.servlet.MockMvc
23-
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
25+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
2426

2527
@WebMvcTest(StudentController::class)
2628
@AutoConfigureRestDocs
@@ -38,23 +40,33 @@ internal class StudentControllerTest {
3840

3941
@Test
4042
fun `학생 등록하기 - CREATED`() {
43+
willDoNothing().given(
44+
studentCreationService.create(
45+
studentFirstName = "진혁",
46+
studentLastName = "",
47+
studentGrade = 3,
48+
studentClassroom = 4,
49+
studentNumber = 17,
50+
)
51+
)
52+
4153
val requestBody = StudentCreationRequest(
4254
student = StudentCreationRequest.StudentInfo(
43-
firstName = "",
44-
lastName = "진혁",
55+
firstName = "진혁",
56+
lastName = "",
4557
grade = 3,
4658
classroom = 4,
4759
number = 17,
4860
)
4961
)
5062

51-
val response = mockMvc.perform(
63+
val result = mockMvc.perform(
5264
createRequest(
5365
httpMethod = HttpMethod.POST,
5466
url = "/students",
5567
requestBody = requestBody,
5668
))
57-
.andExpect(MockMvcResultMatchers.status().isCreated)
69+
.andExpect(status().isCreated)
5870
.andDo(
5971
document(
6072
"학생 등록하기 - CREATED",
@@ -96,5 +108,25 @@ internal class StudentControllerTest {
96108
.response
97109
.contentAsString
98110
.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+
)
128+
129+
assertThat(result.errorCode).isNull()
130+
assertThat(result.errorMessage).isNull()
99131
}
100132
}

0 commit comments

Comments
(0)

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