Home  >  Forum  >  Spring
Post New Query

A ServletContext is required to configure default servlet handling



Asked on May 09, 2015
Hi all,

I am creating a Spring Test class to test spring security.  My configuration class is 
@Configuration 
@ComponentScan("com.test") 
@EnableWebMvc   
@Import({ SecurityConfig.class })
public class AppConfig {  


My spring test class is

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = AppConfig.class)
public class SpringTest {
}

While running I am getting the stacktrace  as below.

Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
at org.springframework.util.Assert.notNull(Assert.java:112) ~[spring-core-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.<init>(DefaultServletHandlerConfigurer.java:54) ~[spring-webmvc-

4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:346) ~[spring-

webmvc-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$7d2f2a82.CGLIB$defaultServletHandlerMapping$25

(<generated>) ~[spring-core-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$7d2f2a82$$FastClassBySpringCGLIB$$fcc3c112.invoke

(<generated>) ~[spring-core-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312) ~[spring-context-

4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$7d2f2a82.defaultServletHandlerMapping(<generated>) 

~[spring-core-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_45]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_45]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_45]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166) ~[spring-beans-

4.0.6.RELEASE.jar:4.0.6.RELEASE]
... 43 common frames omitted






Replied on May 09, 2015
Because of @EnableWebMvc  in configuration class, we need to resolve the loading of DelegatingWebMvcConfiguration in SpringTest class.

Annotate SpringTest  with @WebAppConfiguration


@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = AppConfig.class)
@WebAppConfiguration
public class SpringTest {
}



Replied on May 09, 2015
Solved. Thanks.


Write Answer




©2019 concretepage.com | Privacy Policy | Contact Us