Skip to content

MockMvc should prepare a FilterRegistration when initializing filters #33252

@rstoyanchev

Description

@rstoyanchev

MockMvc supports calling init(FilterConfig) on filters added to it with URL mappings and/or init params and dispatcher types. This was done in 6.1 with #31362.

A filter that's being initialized may in turn try to access the FilterRegistration. Currently that's not initialized, and that can cause problems. For example:

Caused by: java.lang.NullPointerException: Cannot invoke "jakarta.servlet.FilterRegistration.getUrlPatternMappings()" because "filterRegistration" is null
	at org.glassfish.jersey.servlet.init.FilterUrlMappingsProviderImpl.getFilterUrlMappings(FilterUrlMappingsProviderImpl.java:43)
	at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:414)
	at org.springframework.test.web.servlet.setup.MockMvcFilterDecorator.initIfRequired(MockMvcFilterDecorator.java:201)
	at org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder.build(AbstractMockMvcBuilder.java:191)
	at org.springframework.boot.test.autoconfigure.web.servlet.MockMvcAutoConfiguration.mockMvc(MockMvcAutoConfiguration.java:97)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:146)
	... 125 more

We can update MockServletContext to support adding FilterRegistrations, and then make sure that's added in MockMvc along with the FilterConfig it prepares.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions