Class MockMvc
- java.lang.Object
- org.springframework.test.web.servlet.MockMvc
public final class MockMvc extends Object
Main entry point for server-side Spring MVC test support.Example
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; // ... WebApplicationContext wac = ...; MockMvc mockMvc = webAppContextSetup(wac).build(); mockMvc.perform(get("/form")) .andExpect(status().isOk()) .andExpect(content().mimeType("text/html")) .andExpect(forwardedUrl("/WEB-INF/layouts/main.jsp"));
- Since:
- 3.2
- Author:
- Rossen Stoyanchev, Rob Winch, Sam Brannen
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DispatcherServlet
getDispatcherServlet()
Return the underlyingDispatcherServlet
instance that thisMockMvc
was initialized with.ResultActions
perform(RequestBuilder requestBuilder)
Perform a request and return a type that allows chaining further actions, such as asserting expectations, on the result.
Method Detail
getDispatcherServlet
public DispatcherServlet getDispatcherServlet()
Return the underlyingDispatcherServlet
instance that thisMockMvc
was initialized with.This is intended for use in custom request processing scenario where a request handling component happens to delegate to the
DispatcherServlet
at runtime and therefore needs to be injected with it.For most processing scenarios, simply use
perform(org.springframework.test.web.servlet.RequestBuilder)
, or if you need to configure theDispatcherServlet
, provide aDispatcherServletCustomizer
to theMockMvcBuilder
.- Since:
- 5.1
perform
public ResultActions perform(RequestBuilder requestBuilder) throws Exception
Perform a request and return a type that allows chaining further actions, such as asserting expectations, on the result.- Parameters:
requestBuilder
- used to prepare the request to execute; see static factory methods inMockMvcRequestBuilders
- Returns:
- an instance of
ResultActions
(nevernull
) - Throws:
Exception
- See Also:
MockMvcRequestBuilders
,MockMvcResultMatchers