No matter which MockMvc builder you use, all MockMvcBuilder
implementations provide
some common and very useful features. For example, you can declare an Accept
header for
all requests and expect a status of 200 as well as a Content-Type
header in all
responses, as follows:
-
Java
-
Kotlin
// static import of MockMvcBuilders.standaloneSetup
MockMvc mockMvc = standaloneSetup(new MusicController())
.defaultRequest(get("/").accept(MediaType.APPLICATION_JSON))
.alwaysExpect(status().isOk())
.alwaysExpect(content().contentType("application/json;charset=UTF-8"))
.build();
// Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed
In addition, third-party frameworks (and applications) can pre-package setup
instructions, such as those in a MockMvcConfigurer
. The Spring Framework has one such
built-in implementation that helps to save and re-use the HTTP session across requests.
You can use it as follows:
-
Java
-
Kotlin
// static import of SharedHttpSessionConfigurer.sharedHttpSession
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new TestController())
.apply(sharedHttpSession())
.build();
// Use mockMvc to perform requests...
// Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed
See the javadoc for
ConfigurableMockMvcBuilder
for a list of all MockMvc builder features or use the IDE to explore the available options.