You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Spring MVC now supports returning a Mono from REST controller handler methods. And this, in my experience, becomes more and more common because the reactive WebClient tends to be used instead of RestTemplate in MVC applications.
Testing those handler methods with MockMvc requires doing an async dispatch, as documented here.
This is not easy to find out with the MockMvc Java DSL. But when using MockMvc Kotlin DSL, it's even less intuitive. Here's the required code (AFAIK):
val mvcResult =
mockMvc.get(url).andExpect {
request { asyncStarted() }
}.andReturn()
ResultActionsDsl(mockMvc.perform(MockMvcRequestBuilders.asyncDispatch(mvcResult))).andExpect {
status { isOk }
}
As you can see, it requires:
using the static method MockMvcRequestBuilders.asyncDispatch(), whereas the Kotlin DSL is designed to avoid having to use static methods, which are hard to find
making two subsequent calls to mockMvc, which is unintuitive
calling mockMvc.perform(), although the Kotlin DSL usually avoids it thanks to its extension methods (get(), post(), etc.)
creating a new instance of the ResultActionsDsl class explicitly, which is also unintuitive.
I can hide this complexity into an extension function, but I think this function shouldn't be required, and should be part of the DSL itself:
Affects: 5.2.0.RELEASE
Spring MVC now supports returning a Mono from REST controller handler methods. And this, in my experience, becomes more and more common because the reactive WebClient tends to be used instead of RestTemplate in MVC applications.
Testing those handler methods with MockMvc requires doing an async dispatch, as documented here.
This is not easy to find out with the MockMvc Java DSL. But when using MockMvc Kotlin DSL, it's even less intuitive. Here's the required code (AFAIK):
As you can see, it requires:
MockMvcRequestBuilders.asyncDispatch()
, whereas the Kotlin DSL is designed to avoid having to use static methods, which are hard to findmockMvc.perform()
, although the Kotlin DSL usually avoids it thanks to its extension methods (get(), post(), etc.)ResultActionsDsl
class explicitly, which is also unintuitive.I can hide this complexity into an extension function, but I think this function shouldn't be required, and should be part of the DSL itself:
This allows using the following code to write the same test as above:
The text was updated successfully, but these errors were encountered: