spring mvc使用spring-test单元测试失败 Failed to load ApplicationContext

项目是spring + spring mvc,使用spring-test做单元测试,但是一直失败(删掉了部分堆栈信息)

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Bean instantiation via factory method failed; nested exception is 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.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    ... 25 more
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.instantiate(ConstructorResolver.java:651)
    ... 43 more
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:533)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 44 more

提示没有启动容器:

Caused by: java.lang.IllegalStateException: No ServletContext set

项目是纯注解驱动,spring mvc的父子容器都是使用@ComponentScan注解扫描,项目没有spring相关的bean配置文件;
测试类代码:

@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {RootContext.class, AppContext.class})
public class AppTest {

从测试类的代码看出,我把父子容器的配置类都放在了@ContextConfiguration中,但是还是不能启动ServletContext
查阅资料发现需要告诉测试类这是一个web项目,需要在测试类上添加一个@WebAppConfiguration注解,添加注解后项目可以正常测试;

如果还是不能正常测试可以在测试类上的ContextConfiguration添加loader:

// AnnotationConfigWebContextLoader web项目的容器加载器  AnnotationConfigContextLoader 普通项目的容器加载器
@ContextConfiguration(classes = {RootContext.class, AppContext.class}, loader = AnnotationConfigWebContextLoader.class)

如果只需要测试业务代码,而不需要测试controller相关内容,只需如下设置。不用把Controller的扫描器加入到ContextConfiguration中

@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
//@WebAppConfiguration
@ContextConfiguration(classes = {/*AppContext.class, */RootContext.class}/*, loader = AnnotationConfigWebContextLoader.class*/)
public class MerchantOwnerServiceTest {
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容