Add Kotlin DSL support for MockMVC andExpectAll (#29727)
As the DSL internally calls `ResultActions.andExpect`, this is done with a trick where a synthetic `ResultActions` is provided at top level which stores each `ResultMatcher` in a mutable list. Once the DSL usage is done, the top level DSL `andExpectAll` turns that list into a `vararg` passed down to the actual `actions.andExpectAll`. Closes gh-27317
This commit is contained in:
@@ -145,4 +145,12 @@ class MockMvcResultMatchersDsl internal constructor (private val actions: Result
|
||||
fun match(matcher: ResultMatcher) {
|
||||
actions.andExpect(matcher)
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 6.0.4
|
||||
* @see ResultActions.andExpectAll
|
||||
*/
|
||||
fun matchAll(vararg matchers: ResultMatcher) {
|
||||
actions.andExpectAll(*matchers)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,35 @@ class ResultActionsDsl internal constructor (private val actions: ResultActions,
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Provide access to [MockMvcResultMatchersDsl] Kotlin DSL.
|
||||
* @since 6.0.4
|
||||
* @see MockMvcResultMatchersDsl.matchAll
|
||||
*/
|
||||
fun andExpectAll(dsl: MockMvcResultMatchersDsl.() -> Unit): ResultActionsDsl {
|
||||
val softMatchers = mutableListOf<ResultMatcher>()
|
||||
val softActions = object : ResultActions {
|
||||
override fun andExpect(matcher: ResultMatcher): ResultActions {
|
||||
softMatchers.add(matcher)
|
||||
return this
|
||||
}
|
||||
|
||||
override fun andDo(handler: ResultHandler): ResultActions {
|
||||
throw UnsupportedOperationException("andDo should not be part of andExpectAll DSL calls")
|
||||
}
|
||||
|
||||
override fun andReturn(): MvcResult {
|
||||
throw UnsupportedOperationException("andReturn should not be part of andExpectAll DSL calls")
|
||||
}
|
||||
|
||||
}
|
||||
// the use of softActions as the matchers DSL actions parameter will store ResultMatchers in list
|
||||
MockMvcResultMatchersDsl(softActions).dsl()
|
||||
actions.andExpectAll(*softMatchers.toTypedArray())
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide access to [MockMvcResultHandlersDsl] Kotlin DSL.
|
||||
* @see MockMvcResultHandlersDsl.handle
|
||||
|
||||
Reference in New Issue
Block a user