类 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"));
- 从以下版本开始:
- 3.2
- 作者:
- Rossen Stoyanchev, Rob Winch, Sam Brannen
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 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.
方法详细资料
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
.- 从以下版本开始:
- 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.- 参数:
requestBuilder
- used to prepare the request to execute; see static factory methods inMockMvcRequestBuilders
- 返回:
- an instance of
ResultActions
(nevernull
) - 抛出:
Exception
- 另请参阅:
MockMvcRequestBuilders
,MockMvcResultMatchers