Add support for asyncDispatch to MockMvc Kotlin DSL

Closes gh-23758
This commit is contained in:
Sébastien Deleuze
2019-11-22 15:03:17 +01:00
parent 712eac2915
commit dc0ebefc56
3 changed files with 30 additions and 2 deletions

View File

@@ -204,6 +204,6 @@ open class MockHttpServletRequestDsl(private val builder: MockHttpServletRequest
flashAttrs?.also { builder.flashAttrs(flashAttrs!!) } flashAttrs?.also { builder.flashAttrs(flashAttrs!!) }
session?.also { builder.session(session!!) } session?.also { builder.session(session!!) }
principal?.also { builder.principal(principal!!) } principal?.also { builder.principal(principal!!) }
return ResultActionsDsl(mockMvc.perform(builder)) return ResultActionsDsl(mockMvc.perform(builder), mockMvc)
} }
} }

View File

@@ -1,12 +1,14 @@
package org.springframework.test.web.servlet package org.springframework.test.web.servlet
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
/** /**
* Provide a [ResultActions] Kotlin DSL in order to be able to write idiomatic Kotlin code. * Provide a [ResultActions] Kotlin DSL in order to be able to write idiomatic Kotlin code.
* *
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @since 5.2 * @since 5.2
*/ */
class ResultActionsDsl(private val actions: ResultActions) { class ResultActionsDsl(private val actions: ResultActions, private val mockMvc: MockMvc) {
/** /**
* Provide access to [MockMvcResultMatchersDsl] Kotlin DSL. * Provide access to [MockMvcResultMatchersDsl] Kotlin DSL.
@@ -26,6 +28,19 @@ class ResultActionsDsl(private val actions: ResultActions) {
return this return this
} }
/**
* Enable asynchronous dispatching.
* @see MockMvcRequestBuilders.asyncDispatch
* @since 5.2.2
*/
fun asyncDispatch(): ResultActionsDsl {
return andExpect {
request { asyncStarted() }
}.andReturn().let {
ResultActionsDsl(mockMvc.perform(MockMvcRequestBuilders.asyncDispatch(it)), mockMvc)
}
}
/** /**
* @see ResultActions.andReturn * @see ResultActions.andReturn
*/ */

View File

@@ -25,6 +25,7 @@ import org.springframework.http.MediaType.*
import org.springframework.test.web.Person import org.springframework.test.web.Person
import org.springframework.test.web.servlet.setup.MockMvcBuilders import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.bind.annotation.* import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Mono
import java.security.Principal import java.security.Principal
import java.util.* import java.util.*
@@ -153,6 +154,13 @@ class MockMvcExtensionsTests {
} }
} }
@Test
fun asyncDispatch() {
mockMvc.get("/async").asyncDispatch().andExpect {
status { isOk }
}
}
@RestController @RestController
private class PersonController { private class PersonController {
@@ -166,5 +174,10 @@ class MockMvcExtensionsTests {
@PostMapping("/person") @PostMapping("/person")
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
fun post(@RequestBody person: Person) {} fun post(@RequestBody person: Person) {}
@GetMapping("/async")
fun getAsync(): Mono<Person> {
return Mono.just(Person("foo"))
}
} }
} }