/
MockMvcExtensionsTests.kt
183 lines (166 loc) · 5.51 KB
/
MockMvcExtensionsTests.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.web.servlet
import org.assertj.core.api.Assertions.*
import org.hamcrest.CoreMatchers
import org.junit.jupiter.api.Test
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType.*
import org.springframework.test.web.Person
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Mono
import java.security.Principal
import java.util.*
/**
* [MockMvc] DSL tests that verifies builder, actions and expect blocks.
*
* @author Clint Checketts
* @author Petr Balat
* @author Sebastien Deleuze
*/
class MockMvcExtensionsTests {
private val mockMvc = MockMvcBuilders.standaloneSetup(PersonController()).build()
@Test
fun request() {
mockMvc.request(HttpMethod.GET, "/person/{name}", "Lee") {
secure = true
accept = APPLICATION_JSON
headers {
contentLanguage = Locale.FRANCE
}
principal = Principal { "foo" }
}.andExpect {
status { isOk }
content { contentType(APPLICATION_JSON) }
jsonPath("$.name") { value("Lee") }
content { json("""{"someBoolean": false}""", false) }
}.andDo {
print()
}
}
@Test
fun `request without MockHttpServletRequestDsl`() {
mockMvc.request(HttpMethod.GET, "/person/{name}", "Lee").andExpect {
status { isOk }
}.andDo {
print()
}
}
@Test
fun `request with custom matcher and handler`() {
var matcherInvoked = false
var handlerInvoked = false
val matcher = ResultMatcher { matcherInvoked = true }
val handler = ResultHandler { handlerInvoked = true }
mockMvc.request(HttpMethod.GET, "/person/{name}", "Lee").andExpect {
status { isOk }
}.andExpect {
match(matcher)
}.andDo {
handle(handler)
}
assertThat(matcherInvoked).isTrue()
assertThat(handlerInvoked).isTrue()
}
@Test
fun get() {
mockMvc.get("/person/{name}", "Lee") {
secure = true
accept = APPLICATION_JSON
headers {
contentLanguage = Locale.FRANCE
}
principal = Principal { "foo" }
}.andExpect {
status { isOk }
content { contentType(APPLICATION_JSON) }
jsonPath("$.name") { value("Lee") }
content { json("""{"someBoolean": false}""", false) }
}.andDo {
print()
}
}
@Test
fun post() {
mockMvc.post("/person") {
content = """{ "name": "foo" }"""
headers {
accept = listOf(APPLICATION_JSON)
contentType = APPLICATION_JSON
}
}.andExpect {
status {
isCreated
}
}
}
@Test
fun `negative assertion tests to verify the matchers throw errors when expected`() {
val name = "Petr"
mockMvc.get("/person/$name") {
accept = APPLICATION_JSON
}.andExpect {
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { content { contentType(APPLICATION_ATOM_XML) } }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { content { string("Wrong") } }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { jsonPath("name", CoreMatchers.`is`("Wrong")) }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { content { json("""{"name":"wrong"}""") } }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { jsonPath("name") { value("wrong") } }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { cookie { value("name", "wrong") } }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { flash { attribute("name", "wrong") } }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { header { stringValues("name", "wrong") } }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { model { attributeExists("name", "wrong") } }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { redirectedUrl("wrong/Url") }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { redirectedUrlPattern("wrong/Url") }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { status { isAccepted } }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { view { name("wrongName") } }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { jsonPath("name") { value("wrong") } }
}
}
@Test
fun `negative assertion tests for xpath`() {
mockMvc.get("/person/Clint") {
accept = APPLICATION_XML
}.andExpect {
status { isOk }
assertThatExceptionOfType(AssertionError::class.java).isThrownBy { xpath("//wrong") { nodeCount(1) } }
}.andDo {
print()
}
}
@Test
fun asyncDispatch() {
mockMvc.get("/async").asyncDispatch().andExpect {
status { isOk }
}
}
@RestController
private class PersonController {
@GetMapping("/person/{name}")
fun get(@PathVariable name: String): Person {
return Person(name)
}
@Suppress("UNUSED_PARAMETER")
@PostMapping("/person")
@ResponseStatus(HttpStatus.CREATED)
fun post(@RequestBody person: Person) {}
@GetMapping("/async")
fun getAsync(): Mono<Person> {
return Mono.just(Person("foo"))
}
}
}