SpringBootTest 失败 'IllegalStateException - No ServletContext set'
SpringBootTest failing with 'IllegalStateException - No ServletContext set'
我有一个很简单的 @SpringBootTest
:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {...})
public class MyApplicationTest {
@Test
public void anyTest() { ... }
}
它按预期工作,直到我们在 MyApplication.java
文件中添加 @EnableSchedulerLock
(来自 Shedlock)。
因为,我们遇到了这个问题:
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
)之前创建的。
如 Lukas 所述,此错误已在最新版本的 Shedlock 中修复。
将 Shedlock 的版本 从 2.2.0
升级到 2.5.0
解决了这个问题。
我有一个很简单的 @SpringBootTest
:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {...})
public class MyApplicationTest {
@Test
public void anyTest() { ... }
}
它按预期工作,直到我们在 MyApplication.java
文件中添加 @EnableSchedulerLock
(来自 Shedlock)。
因为,我们遇到了这个问题:
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
)之前创建的。
如 Lukas 所述,此错误已在最新版本的 Shedlock 中修复。
将 Shedlock 的版本 从 2.2.0
升级到 2.5.0
解决了这个问题。