Java.lang.IllegalArgumentException: ServletContext required to configure default servlet handling

I have the following test class:

@ActiveProfiles({ "DataTC", "test" }) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {BaseTestConfiguration.class, DataTestConfiguration.class, JpaConfiguration.class, PropertyPlaceholderConfiguration.class }) public class RegularDayToTimeSlotsTest { ... 

The problem seems to come from the BaseTestConfiguration class:

 @Configuration @ComponentScan(basePackages = { "com.bignibou" }, excludeFilters = { @Filter(type = FilterType.CUSTOM, value = RooRegexFilter.class), @Filter(type = FilterType.ANNOTATION, value = Controller.class), @Filter(type = FilterType.ANNOTATION, value = ControllerAdvice.class) }) public class BaseTestConfiguration { } 

I systematically get this exception:

 Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.<init>(DefaultServletHandlerConfigurer.java:54) at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:329) at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$bb4ceb44.CGLIB$defaultServletHandlerMapping$22(<generated>) at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$bb4ceb44$$FastClassByCGLIB$$368bb5c1.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:326) at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$bb4ceb44.defaultServletHandlerMapping(<generated>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166) ... 43 more 

I am not sure how to get around this problem. Somehow Spring is looking for a ServletContext when I run the test and get the exception above ...

+53
spring spring-mvc spring-test
Feb 02
source share
4 answers

One of your @Configuration classes is obviously annotated with @EnableWebMvc . The way DelegatingWebMvcConfiguration ends up in your stack trace, as it is imported using @EnableWebMvc .

So, although you think you don't need a WebApplicationContext (and therefore ServletContext ), you really need this simply because you load the application context using @EnableWebMvc .

You have two options:

  • Compose configuration classes for your integration test so that you do not include Internet-related configuration (i.e. the @Configuration class annotated with @EnableWebMvc ).
  • Annotate your @WebAppConfiguration test class, as suggested in other comments above.

Hello,

Sam (author of Spring TestContext Framework)

+128
Feb 03 '14 at 10:31
source share

It seems that you are missing

 @WebAppConfiguration 

from your test class.

documentation claims

The database path is used behind the scenes to create the MockServletContext, which serves as the ServletContext for WebApplicationContext tests.

Typically, a Servlet container provides a ServletContext . Since you are in a test environment, you need a fake. @WebAppConfiguration provides this.

+35
Feb 03 '14 at 4:27
source share

To create an instance of the servlet context, you will need to use annotation.

 @WebAppConfiguration 

Class-level annotations that are used to declare that the ApplicationContext loaded for the integration test should be a WebApplicationContext. The mere presence of @WebAppConfiguration in the test class ensures that the WebApplicationContext is loaded for the test using the default "file: src / main / webapp" for the path to the root of the web application (i.e., the resource base path). The resource base path is used behind the scenes to create the MockServletContext, which serves as the ServletContext for WebApplicationContext tests.

+10
Feb 03 '14 at 5:19
source share

I was getting a similar error, but when starting the application usually instead of trying to run tests.

It turns out if you are using a custom PermissionEvaluator , then you need to declare it in a separate @Configuration class to someone who has the main Spring security configuration.

See: How to add method-based security to a Spring boot project?

There is also an open Github issue: https://github.com/spring-projects/spring-boot/issues/4875

0
Jul 12 '16 at 11:54 on
source share



All Articles