[JavaEE] Spring单元测试中缺少 ServletContext 错误

来源:互联网 发布:java经典代码大全 编辑:程序博客网 时间:2024/06/05 17:27

在Spring单元测试中出现了如下错误,

Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling

出现上面错误的原因是:
在配置Junit的运行环境的过程中,需要使用@ContextConfiguration(xxx.class)指定@Configuration类。由于指定的类中包含了含有@EnableWebMvc注解的类,所以出现上述错误。

解决办法:

  • 方法1:@ContextConfiguration不要包含带有@EnableWebMvc注解的类
  • 放法2:在测试类中加入@WebApplicationContext注解来配置Servlet环境。

参考:
[1]https://stackoverflow.com/questions/21516683/java-lang-illegalargumentexception-a-servletcontext-is-required-to-configure-de

原创粉丝点击