wang845252276
很倔也很天真
2018-08-25 13:22

SpringMVC Junit4测试 "No ServletContext set"

10
  • spring
  • junit
  • maven
  • mvc

讲真搞了一整天,虽然知道引发错误的是什么,但不能不使用它.


[WARN] method:org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
Exceptionencounteredduringcontextinitialization-cancellingrefreshattempt:org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'resourceHandlerMapping'definedinclasspathresource[org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]:Beaninstantiationviafactorymethodfailed;nestedexceptionisorg.springframework.beans.BeanInstantiationException:Failedtoinstantiate[org.springframework.web.servlet.HandlerMapping]:Factorymethod'resourceHandlerMapping'threwexception;nestedexceptionisjava.lang.IllegalStateException:NoServletContextset
[ERROR]2018-08-2521:01:14,196method:org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:234)
CaughtexceptionwhileallowingTestExecutionListener[org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1c14a519]topreparetestinstance[test.MyTest@3a09d01c]
java.lang.IllegalStateException:FailedtoloadApplicationContext
    atorg.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    atorg.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    atorg.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    atorg.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    atorg.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    atorg.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    atorg.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    atorg.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    atorg.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    atorg.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    atorg.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    atorg.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    atorg.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    atorg.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    atorg.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    atorg.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    atorg.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    atorg.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    atorg.junit.runners.ParentRunner.run(ParentRunner.java:363)
    atorg.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    atorg.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)
    atorg.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)
    atorg.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
    atorg.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)
    atorg.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:383)
    atorg.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:344)
    atorg.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125)
    atorg.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:417)
Causedby:org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'resourceHandlerMapping'definedinclasspathresource[org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]:Beaninstantiationviafactorymethodfailed;nestedexceptionisorg.springframework.beans.BeanInstantiationException:Failedtoinstantiate[org.springframework.web.servlet.HandlerMapping]:Factorymethod'resourceHandlerMapping'threwexception;nestedexceptionisjava.lang.IllegalStateException:NoServletContextset
    atorg.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1181)
    atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1075)
    atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    atorg.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
    atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    atorg.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    atorg.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
    atorg.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    atorg.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:106)
    atorg.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:249)
    atorg.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    atorg.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    ...27more
Causedby:org.springframework.beans.BeanInstantiationException:Failedtoinstantiate[org.springframework.web.servlet.HandlerMapping]:Factorymethod'resourceHandlerMapping'threwexception;nestedexceptionisjava.lang.IllegalStateException:NoServletContextset
    atorg.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    atorg.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ...44more
Causedby:java.lang.IllegalStateException:NoServletContextset
    atorg.springframework.util.Assert.state(Assert.java:70)
    atorg.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:448)
    atorg.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$d8d27a0c.CGLIB$resourceHandlerMapping$36(<generated>)
    atorg.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$d8d27a0c$$FastClassBySpringCGLIB$$7a3e4307.invoke(<generated>)
    atorg.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    atorg.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
    atorg.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$d8d27a0c.resourceHandlerMapping(<generated>)
    atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
    atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    atjava.lang.reflect.Method.invoke(Method.java:606)
    atorg.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ...45more
[ERROR]Testsrun:1,Failures:0,Errors:1,Skipped:0,Timeelapsed:0.787s<<<FAILURE!-intest.MyTest
[ERROR]testSelect(test.MyTest)Timeelapsed:0.001s<<<ERROR!
java.lang.IllegalStateException:FailedtoloadApplicationContext
Causedby:org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'resourceHandlerMapping'definedinclasspathresource[org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]:Beaninstantiationviafactorymethodfailed;nestedexceptionisorg.springframework.beans.BeanInstantiationException:Failedtoinstantiate[org.springframework.web.servlet.HandlerMapping]:Factorymethod'resourceHandlerMapping'threwexception;nestedexceptionisjava.lang.IllegalStateException:NoServletContextset
Causedby:org.springframework.beans.BeanInstantiationException:Failedtoinstantiate[org.springframework.web.servlet.HandlerMapping]:Factorymethod'resourceHandlerMapping'threwexception;nestedexceptionisjava.lang.IllegalStateException:NoServletContextset
Causedby:java.lang.IllegalStateException:NoServletContextset

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答

为你推荐