Сбой SpringBootTest с «IllegalStateException - No ServletContext set» - PullRequest
0 голосов
/ 24 мая 2019

У меня довольно простое @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).

1 Ответ

0 голосов
/ 29 мая 2019

Как упоминал Лукас, эта ошибка была исправлена ​​в последней версии Shedlock.

Обновление версии Shedlock с 2.2.0 до 2.5.0 устраняет проблему.

...