Currently, when a Servlet Filter is added to MockMvc, its init(FilterConfig) method isn't called. This causes a problem for certain filters and doesn't align with the Servlet spec which requires init to be called before doFilter is called. While the caller that's adding the filter could call init, it's cumbersome as FilterConfig requires a ServletContext and there's no such context until the MockMvc instance has been built. It works with the following:

TestFilter testFilter = new TestFilter();
MockMvc mockMvc = MockMvcBuilders.standaloneSetup().addFilter(testFilter).build();
testFilter.init(new MockFilterConfig(mockMvc.getDispatcherServlet().getServletContext()));
mockMvc.perform(MockMvcRequestBuilders.get("/"));

I would like to see the builder streamline this by taking care of filter initialization. The existing methods for adding a filter could, perhaps, call init with an empty filter config that contains no init parameters. addFilter could then also be overloaded to allow init parameters to be passed in.

This suggestion is quite closely related to https://github.com/spring-projects/spring-framework/issues/27717 that proposes adding some support for registering filters for particular dispatcher types. Both would be very useful for Spring Boot's auto-configuring of MockMvc where any filters in the context are automatically added. Where those filters are represented by a FilterRegistrationBean we have information about dispatcher types and init parameters on hand that we could pass down into MockMvc if the API to do so was available.