diff --git a/spring-test/src/main/kotlin/org/springframework/test/web/servlet/ResultActionsDsl.kt b/spring-test/src/main/kotlin/org/springframework/test/web/servlet/ResultActionsDsl.kt index 3528391f5a17..43c639742a20 100644 --- a/spring-test/src/main/kotlin/org/springframework/test/web/servlet/ResultActionsDsl.kt +++ b/spring-test/src/main/kotlin/org/springframework/test/web/servlet/ResultActionsDsl.kt @@ -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 } @@ -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 } diff --git a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt index a15484408d48..8213a1438cb0 100644 --- a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt +++ b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt @@ -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.* @@ -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() } } @@ -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) @@ -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() } } @@ -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 } @@ -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 { content { contentType(MediaType.APPLICATION_ATOM_XML) } } + accept = APPLICATION_JSON + }.andExpect { + assertThrows { content { contentType(APPLICATION_ATOM_XML) } } assertThrows { content { string("Wrong") } } assertThrows { jsonPath("name", CoreMatchers.`is`("Wrong")) } assertThrows { content { json("""{"name":"wrong"}""") } } @@ -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 { xpath("//wrong") { nodeCount(1) } } - } andDo { + }.andDo { print() } } diff --git a/src/docs/asciidoc/languages/kotlin.adoc b/src/docs/asciidoc/languages/kotlin.adoc index 17f9547e46a4..bfec9fb050d1 100644 --- a/src/docs/asciidoc/languages/kotlin.adoc +++ b/src/docs/asciidoc/languages/kotlin.adoc @@ -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() } ----