java.lang.IllegalArgumentException:需要ServletContext来配置默认的servlet处理

bal*_*teo 55 spring spring-mvc spring-test

我有以下测试类:

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

问题似乎来自BaseTestConfiguration类:

@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 {

}
Run Code Online (Sandbox Code Playgroud)

我系统地得到这个例外:

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
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决这个问题.不知怎的,当我运行测试时,Spring正在寻找一个ServletContext,我得到了上述异常......

Sam*_*nen 139

你的一个@Configuration课程显然是注释的@EnableWebMvc.这就是DelegatingWebMvcConfiguration堆栈跟踪中的最终结果,因为它是由导入@EnableWebMvc.

因此,虽然您认为您不需要WebApplicationContext(因此也不需要ServletContext),但实际上您确实需要它,因为您正在加载应用程序上下文@EnableWebMvc.

您有两种选择:

  • 编写集成测试的配置类,以便不包括与Web相关的配置(即,使用@Configuration注释的类@EnableWebMvc).
  • 使用@WebAppConfiguration上面其他注释中的建议来注释您的测试类.

问候,

Sam(Spring TestContext Framework的作者)

  • 确保使用AnnotationConfigWebContextLoader(如果使用配置类)加载上下文,如下所示:`@ContextConfiguration(loader = AnnotationConfigWebContextLoader.class,classes = {ConfigClass.class})` (6认同)

Sot*_*lis 38

好像你错过了

@WebAppConfiguration
Run Code Online (Sandbox Code Playgroud)

来自你的考试班.

文档的状态

在后台使用资源基本路径来创建MockServletContext,它充当测试的WebApplicationContext的ServletContext.

通常,Servlet容器会提供ServletContext.由于您处于测试环境中,因此您需要假货.@WebAppConfiguration提供了.


Hri*_*esh 11

要实例化Servlet上下文,您必须使用注释.

@WebAppConfiguration
Run Code Online (Sandbox Code Playgroud)

一个类级别注释,用于声明为集成测试加载的ApplicationContext应该是WebApplicationContext.仅在测试类上存在@WebAppConfiguration可确保为测试加载WebApplicationContext,使用默认值"file:src/main/webapp"作为Web应用程序根目录的路径(即资源)基本路径).在后台使用资源基本路径来创建MockServletContext,它充当测试的WebApplicationContext的ServletContext.