java.lang.IllegalArgumentException:ServletContext需要configuration默认的servlet处理

我有以下testing类:

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

这个问题似乎来自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 { } 

我系统地得到这个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 

我不知道如何解决这个问题。 不知何故spring正在寻找一个ServletContext,当我运行testing,我得到了上述例外…

你的一个@Configuration类显然是用@EnableWebMvc注解的。 这就是DelegatingWebMvcConfiguration在堆栈跟踪中的结果,因为它是由@EnableWebMvc 导入的。

因此,尽pipe您认为不需要WebApplicationContext (因此也不需要ServletContext ),但事实上确实需要它,只是因为您正在使用@EnableWebMvc加载应用程序上下文。

你有两个select:

  • 为您的集成testing编写configuration类,以便不包含与Web相关的configuration(即,使用@EnableWebMvc注释的@Configuration类)。
  • 按照上面其他注释中的build议,使用@WebAppConfiguration注释您的testing类。

问候,

Sam(Spring TestContext框架的作者)

看来你好像缺less了

 @WebAppConfiguration 

来自你的testingclass。

文档说明

资源库path在后台使用,创build一个MockServletContext,作为testing的WebApplicationContext的ServletContext。

通常一个Servlet容器会提供ServletContext 。 由于您处于testing环境中,您需要一个假的。 @WebAppConfiguration提供了。

为了实例化Servlet上下文,您必须使用注释。

 @WebAppConfiguration 

用于声明为集成testing加载的ApplicationContext应该是WebApplicationContext的类级注释。 在testing类中仅存在@WebAppConfiguration可确保为testing加载WebApplicationContext,使用默认值“file:src / main / webapp”作为Web应用程序根目录的path(即资源基本path)。 资源库path在后台使用,创build一个MockServletContext,作为testing的WebApplicationContext的ServletContext。

我得到了类似的错误,但正常运行应用程序,而不是试图运行testing。

原来,如果你正在使用一个自定义的PermissionEvaluator那么你需要在一个单独的@Configuration类中声明它到你的主要Spring安全configuration中。

请参阅: 如何将基于方法的安全性添加到Spring Boot项目?

还有一个开放的Github问题: https : //github.com/spring-projects/spring-boot/issues/4875