Allow Filters to be initialized when they're added to MockMvc #31362
Labels
in: test
Issues in the test module
in: web
Issues in web modules (web, webmvc, webflux, websocket)
type: enhancement
A general enhancement
Milestone
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 requiresinit
to be called beforedoFilter
is called. While the caller that's adding the filter could callinit
, it's cumbersome asFilterConfig
requires aServletContext
and there's no such context until theMockMvc
instance has been built. It works with the following: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 #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 aFilterRegistrationBean
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.The text was updated successfully, but these errors were encountered: