Skip to content

Commit

Permalink
Remove infix qualifier from ResultActionsDsl methods
Browse files Browse the repository at this point in the history
The infix form limits the extensibility of the API and
prevents calling `andReturn()`.

See spring-projectsgh-1951
  • Loading branch information
sdeleuze committed Mar 4, 2019
1 parent 0c33228 commit 9e873af
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ResultActionsDsl(private val actions: ResultActions) {
* Provide access to [MockMvcResultMatchersDsl] Kotlin DSL.
* @see MockMvcResultMatchersDsl.match
*/
infix fun andExpect(dsl: MockMvcResultMatchersDsl.() -> Unit): ResultActionsDsl {
fun andExpect(dsl: MockMvcResultMatchersDsl.() -> Unit): ResultActionsDsl {
MockMvcResultMatchersDsl(actions).dsl()
return this
}
Expand All @@ -21,7 +21,7 @@ class ResultActionsDsl(private val actions: ResultActions) {
* Provide access to [MockMvcResultHandlersDsl] Kotlin DSL.
* @see MockMvcResultHandlersDsl.handle
*/
infix fun andDo(dsl: MockMvcResultHandlersDsl.() -> Unit): ResultActionsDsl {
fun andDo(dsl: MockMvcResultHandlersDsl.() -> Unit): ResultActionsDsl {
MockMvcResultHandlersDsl(actions).dsl()
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.junit.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
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.*
Expand All @@ -44,26 +44,26 @@ class MockMvcExtensionsTests {
fun request() {
mockMvc.request(HttpMethod.GET, "/person/{name}", "Lee") {
secure = true
accept = MediaType.APPLICATION_JSON
accept = APPLICATION_JSON
headers {
contentLanguage = Locale.FRANCE
}
principal = Principal { "foo" }
} andExpect {
}.andExpect {
status { isOk }
content { contentType("application/json;charset=UTF-8") }
content { contentType(APPLICATION_JSON_UTF8) }
jsonPath("$.name") { value("Lee") }
content { json("""{"someBoolean": false}""", false) }
} andDo {
}.andDo {
print()
}
}

@Test
fun `request without MockHttpServletRequestDsl`() {
mockMvc.request(HttpMethod.GET, "/person/{name}", "Lee") andExpect {
mockMvc.request(HttpMethod.GET, "/person/{name}", "Lee").andExpect {
status { isOk }
} andDo {
}.andDo {
print()
}
}
Expand All @@ -74,11 +74,11 @@ class MockMvcExtensionsTests {
var handlerInvoked = false
val matcher = ResultMatcher { matcherInvoked = true }
val handler = ResultHandler { handlerInvoked = true }
mockMvc.request(HttpMethod.GET, "/person/{name}", "Lee") andExpect {
mockMvc.request(HttpMethod.GET, "/person/{name}", "Lee").andExpect {
status { isOk }
} andExpect {
}.andExpect {
match(matcher)
} andDo {
}.andDo {
handle(handler)
}
Assert.assertTrue(matcherInvoked)
Expand All @@ -89,17 +89,17 @@ class MockMvcExtensionsTests {
fun get() {
mockMvc.get("/person/{name}", "Lee") {
secure = true
accept = MediaType.APPLICATION_JSON
accept = APPLICATION_JSON_UTF8
headers {
contentLanguage = Locale.FRANCE
}
principal = Principal { "foo" }
} andExpect {
}.andExpect {
status { isOk }
content { contentType("application/json;charset=UTF-8") }
content { contentType(APPLICATION_JSON_UTF8) }
jsonPath("$.name") { value("Lee") }
content { json("""{"someBoolean": false}""", false) }
} andDo {
}.andDo {
print()
}
}
Expand All @@ -109,10 +109,10 @@ class MockMvcExtensionsTests {
mockMvc.post("/person") {
content = """{ "name": "foo" }"""
headers {
accept = listOf(MediaType.APPLICATION_JSON)
contentType = MediaType.APPLICATION_JSON
accept = listOf(APPLICATION_JSON)
contentType = APPLICATION_JSON
}
} andExpect {
}.andExpect {
status {
isCreated
}
Expand All @@ -123,9 +123,9 @@ class MockMvcExtensionsTests {
fun `negative assertion tests to verify the matchers throw errors when expected`() {
val name = "Petr"
mockMvc.get("/person/$name") {
accept = MediaType.APPLICATION_JSON
} andExpect {
assertThrows<AssertionError> { content { contentType(MediaType.APPLICATION_ATOM_XML) } }
accept = APPLICATION_JSON
}.andExpect {
assertThrows<AssertionError> { content { contentType(APPLICATION_ATOM_XML) } }
assertThrows<AssertionError> { content { string("Wrong") } }
assertThrows<AssertionError> { jsonPath("name", CoreMatchers.`is`("Wrong")) }
assertThrows<AssertionError> { content { json("""{"name":"wrong"}""") } }
Expand All @@ -146,11 +146,11 @@ class MockMvcExtensionsTests {
@Test
fun `negative assertion tests for xpath`() {
mockMvc.get("/person/Clint") {
accept = MediaType.APPLICATION_XML
} andExpect {
accept = APPLICATION_XML
}.andExpect {
status { isOk }
assertThrows<AssertionError> { xpath("//wrong") { nodeCount(1) } }
} andDo {
}.andDo {
print()
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/docs/asciidoc/languages/kotlin.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -336,17 +336,17 @@ better discoverability (no usage of static methods).
val mockMvc: MockMvc = ...
mockMvc.get("/person/{name}", "Lee") {
secure = true
accept = MediaType.APPLICATION_JSON
accept = APPLICATION_JSON
headers {
contentLanguage = Locale.FRANCE
}
principal = Principal { "foo" }
} andExpect {
}.andExpect {
status { isOk }
content { contentType("application/json;charset=UTF-8") }
content { contentType(APPLICATION_JSON_UTF8) }
jsonPath("$.name") { value("Lee") }
content { json("""{"someBoolean": false}""", false) }
} andDo {
}.andDo {
print()
}
----
Expand Down

0 comments on commit 9e873af

Please sign in to comment.