Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MockMvc should prepare a FilterRegistration when initializing filters #33252

Closed
rstoyanchev opened this issue Jul 22, 2024 · 1 comment
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@rstoyanchev
Copy link
Contributor

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.

@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement labels Jul 22, 2024
@rstoyanchev rstoyanchev added this to the 6.2.0-M7 milestone Jul 22, 2024
@rstoyanchev rstoyanchev self-assigned this Jul 22, 2024
@wilkinsona
Copy link
Member

The stack trace above shows this affecting users of Jersey. The arrangement here is an app that's using both Spring MVC and Jersey and is trying to use MockMvc to test the Spring MVC portion of the app. In this situation, the problem can be worked around by disabling Jersey during MockMvc-based tests of the Spring MVC portion of the app.

If Jersey is being configured using Spring Boot's auto-configuration, it can be disabled by excluded JerseyAutoConfiguration in the tests that are using @AutoConfigureMockMvc.

If Jersey's being configured by some other mechanism, disabling the bean that registers Jersey's filter will achieve the same effect. This can be done using a BeanPostProcessor that acts upon the registration bean for Jersey's filter. For example, assuming that the registration bean is named jerseyFilterRegistration:

@Bean
static BeanPostProcessor disableJerseyFilter() {
	return new BeanPostProcessor() {

		@Override
		public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
			if (beanName.equals("jerseyFilterRegistration")) {
				((FilterRegistrationBean<?>) bean).setEnabled(false);
			}
			return bean;
		}

	};
}

rstoyanchev added a commit that referenced this issue Jul 31, 2024
Follow-up to d2225c, which broke Boot tests because the ServletContext can
be the one of the embedded Servlet container if initializing a live server
and as well as MockMvc (e.g. via `@AutoConfigureMockMvc`).

See gh-33252
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants