MockMvcBuilders is a factory class in Spring Test that builds a MockMvc instance. It offers two entry points: webAppContextSetup(...) builds MockMvc from a full Spring WebApplicationContext, and standaloneSetup(...) builds it from one or more controllers you create yourself, without loading a Spring context.
In a Spring Boot application you often do not call it directly, because @WebMvcTest and @AutoConfigureMockMvc create and inject a configured MockMvc for you. You use MockMvcBuilders when you want finer control or very fast tests.
MockMvc simulates HTTP requests against Spring MVC without starting a server. It runs the real DispatcherServlet, handler mappings, argument resolvers, validation and message converters, but with mock servlet request and response objects. MockMvcBuilders is the class that assembles that setup and returns a builder you finish with build().
The class lives in org.springframework.test.web.servlet.setup and has these static methods:
| Method | Builds from | Typical use |
|---|---|---|
webAppContextSetup(WebApplicationContext) | A loaded application context | Integration tests with real beans, security, advice |
standaloneSetup(Object...) | Controller instances you pass in | Fast unit tests of controllers |
standaloneSetup registers only the controllers you provide, with default MVC configuration. Dependencies are plain mocks, so no Spring context is started.
Standalone mode does not load your @ControllerAdvice, security filters or custom converters unless you add them with setControllerAdvice(...), addFilters(...) or setMessageConverters(...).
webAppContextSetup uses a real WebApplicationContext, so exception handlers, validators, interceptors and security configuration behave as in production.
apply(...) accepts a MockMvcConfigurer. Spring Security's springSecurity() adds the filter chain, which is the supported way to test secured endpoints.
Both builders extend AbstractMockMvcBuilder, so they share options:
addFilters(Filter...) adds servlet filters such as a CORS or logging filter.alwaysExpect(ResultMatcher) applies an assertion to every request, for example a content type.alwaysDo(ResultHandler) runs a handler on every call, such as print().defaultRequest(RequestBuilder) sets headers or parameters used by every request.The standalone builder also supports setControllerAdvice, setValidator, setCustomArgumentResolvers and setMessageConverters.
With Spring Boot, prefer the annotations unless you need standalone mode:
@WebMvcTest loads only the web layer and auto-configures MockMvc through the same machinery as webAppContextSetup. @MockitoBean replaces the deprecated @MockBean from Spring Framework 6.2 onward.
standaloneSetup builds MVC infrastructure around controller instances you supply, which is fast but limited. webAppContextSetup uses a loaded context, so the test sees the same beans and configuration as the application.
Usually not. @WebMvcTest or @AutoConfigureMockMvc injects a ready MockMvc. Use MockMvcBuilders for plain unit tests or custom setups.
No. It processes requests in memory using mock request and response objects. For a real server and HTTP stack, use @SpringBootTest(webEnvironment = RANDOM_PORT) with TestRestTemplate or WebTestClient.
In standalone mode they are not discovered. Register them with setControllerAdvice(...).
MockMvcBuilders creates MockMvc instances, either from a full application context or from controllers you supply. Choose standaloneSetup for speed and isolation, webAppContextSetup for realistic behaviour, and rely on @WebMvcTest when working inside Spring Boot.