【问题标题】:SpringBootTest failing with 'IllegalStateException - No ServletContext set'SpringBootTest 因“IllegalStateException - 未设置 ServletContext”而失败
【发布时间】:2019-05-24 13:14:45
【问题描述】:

我有一个非常简单的@SpringBootTest

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {...})
public class MyApplicationTest {

    @Test
    public void anyTest() { ... }
}

在我们将@EnableSchedulerLock(来自Shedlock)添加到MyApplication.java 文件之前,它一直按预期工作。

因为,我们遇到了这个问题:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582)
    ... 46 common frames omitted
Caused by: java.lang.IllegalStateException: No ServletContext set
    at org.springframework.util.Assert.state(Assert.java:73)
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:483)

这发生在 Spring 尝试实例化其 resourceHandlerMapping 时:

@Bean
public HandlerMapping resourceHandlerMapping() {
    Assert.state(this.applicationContext != null, "No ApplicationContext set");
    Assert.state(this.servletContext != null, "No ServletContext set");
    ...

就像@Bean 是在调用setServletContext(来自WebMvcConfigurationSupport)之前创建的。

【问题讨论】:

  • 您好,您使用哪个 ShedLock 版本?它不应该发生在最近的一个。也可能有另一个异常是根本原因。
  • 我们使用的是 2.2.0,我尝试将其升级到 2.5.0,正如您所说,没有更多例外。谢谢。您可以将此作为答案发布。

标签: spring spring-boot spring-boot-test shedlock


【解决方案1】:

正如 Lukas 所说,这个错误已在最新版本的 Shedlock 中得到修复。

升级 Shedlock 的版本2.2.02.5.0 解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多