Skip to content

Commit 9e873af

Browse files
committed
Remove infix qualifier from ResultActionsDsl methods
The infix form limits the extensibility of the API and prevents calling `andReturn()`. See spring-projectsgh-1951
1 parent 0c33228 commit 9e873af

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

spring-test/src/main/kotlin/org/springframework/test/web/servlet/ResultActionsDsl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ResultActionsDsl(private val actions: ResultActions) {
1212
* Provide access to [MockMvcResultMatchersDsl] Kotlin DSL.
1313
* @see MockMvcResultMatchersDsl.match
1414
*/
15-
infix fun andExpect(dsl: MockMvcResultMatchersDsl.() -> Unit): ResultActionsDsl {
15+
fun andExpect(dsl: MockMvcResultMatchersDsl.() -> Unit): ResultActionsDsl {
1616
MockMvcResultMatchersDsl(actions).dsl()
1717
return this
1818
}
@@ -21,7 +21,7 @@ class ResultActionsDsl(private val actions: ResultActions) {
2121
* Provide access to [MockMvcResultHandlersDsl] Kotlin DSL.
2222
* @see MockMvcResultHandlersDsl.handle
2323
*/
24-
infix fun andDo(dsl: MockMvcResultHandlersDsl.() -> Unit): ResultActionsDsl {
24+
fun andDo(dsl: MockMvcResultHandlersDsl.() -> Unit): ResultActionsDsl {
2525
MockMvcResultHandlersDsl(actions).dsl()
2626
return this
2727
}

spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.junit.Test
2222
import org.junit.jupiter.api.assertThrows
2323
import org.springframework.http.HttpMethod
2424
import org.springframework.http.HttpStatus
25-
import org.springframework.http.MediaType
25+
import org.springframework.http.MediaType.*
2626
import org.springframework.test.web.Person
2727
import org.springframework.test.web.servlet.setup.MockMvcBuilders
2828
import org.springframework.web.bind.annotation.*
@@ -44,26 +44,26 @@ class MockMvcExtensionsTests {
4444
fun request() {
4545
mockMvc.request(HttpMethod.GET, "/person/{name}", "Lee") {
4646
secure = true
47-
accept = MediaType.APPLICATION_JSON
47+
accept = APPLICATION_JSON
4848
headers {
4949
contentLanguage = Locale.FRANCE
5050
}
5151
principal = Principal { "foo" }
52-
} andExpect {
52+
}.andExpect {
5353
status { isOk }
54-
content { contentType("application/json;charset=UTF-8") }
54+
content { contentType(APPLICATION_JSON_UTF8) }
5555
jsonPath("$.name") { value("Lee") }
5656
content { json("""{"someBoolean": false}""", false) }
57-
} andDo {
57+
}.andDo {
5858
print()
5959
}
6060
}
6161

6262
@Test
6363
fun `request without MockHttpServletRequestDsl`() {
64-
mockMvc.request(HttpMethod.GET, "/person/{name}", "Lee") andExpect {
64+
mockMvc.request(HttpMethod.GET, "/person/{name}", "Lee").andExpect {
6565
status { isOk }
66-
} andDo {
66+
}.andDo {
6767
print()
6868
}
6969
}
@@ -74,11 +74,11 @@ class MockMvcExtensionsTests {
7474
var handlerInvoked = false
7575
val matcher = ResultMatcher { matcherInvoked = true }
7676
val handler = ResultHandler { handlerInvoked = true }
77-
mockMvc.request(HttpMethod.GET, "/person/{name}", "Lee") andExpect {
77+
mockMvc.request(HttpMethod.GET, "/person/{name}", "Lee").andExpect {
7878
status { isOk }
79-
} andExpect {
79+
}.andExpect {
8080
match(matcher)
81-
} andDo {
81+
}.andDo {
8282
handle(handler)
8383
}
8484
Assert.assertTrue(matcherInvoked)
@@ -89,17 +89,17 @@ class MockMvcExtensionsTests {
8989
fun get() {
9090
mockMvc.get("/person/{name}", "Lee") {
9191
secure = true
92-
accept = MediaType.APPLICATION_JSON
92+
accept = APPLICATION_JSON_UTF8
9393
headers {
9494
contentLanguage = Locale.FRANCE
9595
}
9696
principal = Principal { "foo" }
97-
} andExpect {
97+
}.andExpect {
9898
status { isOk }
99-
content { contentType("application/json;charset=UTF-8") }
99+
content { contentType(APPLICATION_JSON_UTF8) }
100100
jsonPath("$.name") { value("Lee") }
101101
content { json("""{"someBoolean": false}""", false) }
102-
} andDo {
102+
}.andDo {
103103
print()
104104
}
105105
}
@@ -109,10 +109,10 @@ class MockMvcExtensionsTests {
109109
mockMvc.post("/person") {
110110
content = """{ "name": "foo" }"""
111111
headers {
112-
accept = listOf(MediaType.APPLICATION_JSON)
113-
contentType = MediaType.APPLICATION_JSON
112+
accept = listOf(APPLICATION_JSON)
113+
contentType = APPLICATION_JSON
114114
}
115-
} andExpect {
115+
}.andExpect {
116116
status {
117117
isCreated
118118
}
@@ -123,9 +123,9 @@ class MockMvcExtensionsTests {
123123
fun `negative assertion tests to verify the matchers throw errors when expected`() {
124124
val name = "Petr"
125125
mockMvc.get("/person/$name") {
126-
accept = MediaType.APPLICATION_JSON
127-
} andExpect {
128-
assertThrows<AssertionError> { content { contentType(MediaType.APPLICATION_ATOM_XML) } }
126+
accept = APPLICATION_JSON
127+
}.andExpect {
128+
assertThrows<AssertionError> { content { contentType(APPLICATION_ATOM_XML) } }
129129
assertThrows<AssertionError> { content { string("Wrong") } }
130130
assertThrows<AssertionError> { jsonPath("name", CoreMatchers.`is`("Wrong")) }
131131
assertThrows<AssertionError> { content { json("""{"name":"wrong"}""") } }
@@ -146,11 +146,11 @@ class MockMvcExtensionsTests {
146146
@Test
147147
fun `negative assertion tests for xpath`() {
148148
mockMvc.get("/person/Clint") {
149-
accept = MediaType.APPLICATION_XML
150-
} andExpect {
149+
accept = APPLICATION_XML
150+
}.andExpect {
151151
status { isOk }
152152
assertThrows<AssertionError> { xpath("//wrong") { nodeCount(1) } }
153-
} andDo {
153+
}.andDo {
154154
print()
155155
}
156156
}

src/docs/asciidoc/languages/kotlin.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,17 +336,17 @@ better discoverability (no usage of static methods).
336336
val mockMvc: MockMvc = ...
337337
mockMvc.get("/person/{name}", "Lee") {
338338
secure = true
339-
accept = MediaType.APPLICATION_JSON
339+
accept = APPLICATION_JSON
340340
headers {
341341
contentLanguage = Locale.FRANCE
342342
}
343343
principal = Principal { "foo" }
344-
} andExpect {
344+
}.andExpect {
345345
status { isOk }
346-
content { contentType("application/json;charset=UTF-8") }
346+
content { contentType(APPLICATION_JSON_UTF8) }
347347
jsonPath("$.name") { value("Lee") }
348348
content { json("""{"someBoolean": false}""", false) }
349-
} andDo {
349+
}.andDo {
350350
print()
351351
}
352352
----

0 commit comments

Comments
 (0)