tianyirao
2013-09-24 17:02
浏览 917
已采纳

junit测试报Failed to load ApplicationContext

需要测试src里面的service、action等;新建了个src文件夹命名为“testCase”,和src同级;applicationContext.xml文件在src里面,不太清楚除了需要加载这个配置文件还需要啥,看了网上例子用了如下的代码:
@ContextConfiguration(locations={"classpath:applicationContext.xml"})

执行test报:
log4j:WARN No such property [maxFileSize] in org.apache.log4j.DailyRollingFileAppender.
log4j:WARN No such property [maxBackupIndex] in org.apache.log4j.DailyRollingFileAppender.
2013-09-24 16:56:39 [org.springframework.test.context.TestContextManager.retrieveTestExecutionListeners(TestContextManager.java:185)]-[INFO] @TestExecutionListeners is not present for class [class loginTest]: using defaults.
2013-09-24 16:56:39 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:315)]-[INFO] Loading XML bean definitions from class path resource [applicationContext.xml]
2013-09-24 16:56:39 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:315)]-[INFO] Loading XML bean definitions from class path resource [sdmob-gis.xml]
2013-09-24 16:56:39 [org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:456)]-[INFO] Refreshing org.springframework.context.support.GenericApplicationContext@1bd0dd4: startup date [Tue Sep 24 16:56:39 CST 2013]; root of context hierarchy
2013-09-24 16:56:40 [org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner.checkCandidate(MapperScannerConfigurer.java:453)]-[WARN] Skipping MapperFactoryBean with name 'alarmInfoDao' and 'com.nr.sd.mob.dao.AlarmInfoDao' mapperInterface. Bean already defined with the same name!
2013-09-24 16:56:40 [org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner.checkCandidate(MapperScannerConfigurer.java:453)]-[WARN] Skipping MapperFactoryBean with name 'attemperGraphDao' and 'com.nr.sd.mob.dao.AttemperGraphDao' mapperInterface. Bean already defined with the same name!
2013-09-24 16:56:40 [org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner.checkCandidate(MapperScannerConfigurer.java:453)]-[WARN] Skipping MapperFactoryBean with name 'curveDao' and 'com.nr.sd.mob.dao.CurveDao' mapperInterface. Bean already defined with the same name!
2013-09-24 16:56:40 [org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner.checkCandidate(MapperScannerConfigurer.java:453)]-[WARN] Skipping MapperFactoryBean with name 'damCtrlFloodDao' and 'com.nr.sd.mob.dao.DamCtrlFloodDao' mapperInterface. Bean already defined with the same name!
2013-09-24 16:56:40 [org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner.checkCandidate(MapperScannerConfigurer.java:453)]-[WARN] Skipping MapperFactoryBean with name 'mobMenuDao' and 'com.nr.sd.mob.dao.MobMenuDao' mapperInterface. Bean already defined with the same name!
2013-09-24 16:56:40 [org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner.checkCandidate(MapperScannerConfigurer.java:453)]-[WARN] Skipping MapperFactoryBean with name 'mobUserDao' and 'com.nr.sd.mob.dao.MobUserDao' mapperInterface. Bean already defined with the same name!
2013-09-24 16:56:40 [org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner.checkCandidate(MapperScannerConfigurer.java:453)]-[WARN] Skipping MapperFactoryBean with name 'reportSrhDao' and 'com.nr.sd.mob.dao.ReportSrhDao' mapperInterface. Bean already defined with the same name!
2013-09-24 16:56:40 [org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner.doScan(MapperScannerConfigurer.java:397)]-[WARN] No MyBatis mapper was found in 'com.nr.sd.mob.dao' package. Please check your configuration.
2013-09-24 16:56:40 [org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:555)]-[INFO] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@530cf2: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,weatherImageAction,monitorAction,weatherImageJobDetail,monitorJobDetail,simpleTrigger,monitorTrigger,scheduleFactory,dataSource,sqlSessionFactory,org.mybatis.spring.mapper.MapperScannerConfigurer#0,mobUserDao,mobUserLoginAction,mobUserService,mobMenuDao,mobMenuAction,mobMenuService,damCtrlFloodDao,damCtrlFloodService,DamConfigAction,DamQueryAction,WaterRegimeAction,SingleFetchAction,QueryFetchAction,curveDao,curveService,CurveQueryAction,attemperRuleService,AttemperRuleAction,attemperGraphDao,attemperGraphService,AttemperGraphAction,alarmInfoDao,alarmInfoAction,alarmInfoService,TopologyGraphAction,HydroOverviewAction,TestInterfaceAction,reportSrhDao,reportSrhAction,reportSrhService,readHyDispXmlAction,readHyDispXmlService,LonlatAction,StationAction,readHyDispRFLXmlAction,readHyDispRFLXmlService,getZoomLevelAction,getZoomLevelService,querySubStreamAction,querySubStreamService,querySubStreamDao,getSubStreamRFLAction,getIsoLinePicAction,getIsoLinePicService,getIsoLinePicDao]; root of factory hierarchy
2013-09-24 16:56:40 [org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:422)]-[INFO] Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@530cf2: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,weatherImageAction,monitorAction,weatherImageJobDetail,monitorJobDetail,simpleTrigger,monitorTrigger,scheduleFactory,dataSource,sqlSessionFactory,org.mybatis.spring.mapper.MapperScannerConfigurer#0,mobUserDao,mobUserLoginAction,mobUserService,mobMenuDao,mobMenuAction,mobMenuService,damCtrlFloodDao,damCtrlFloodService,DamConfigAction,DamQueryAction,WaterRegimeAction,SingleFetchAction,QueryFetchAction,curveDao,curveService,CurveQueryAction,attemperRuleService,AttemperRuleAction,attemperGraphDao,attemperGraphService,AttemperGraphAction,alarmInfoDao,alarmInfoAction,alarmInfoService,TopologyGraphAction,HydroOverviewAction,TestInterfaceAction,reportSrhDao,reportSrhAction,reportSrhService,readHyDispXmlAction,readHyDispXmlService,LonlatAction,StationAction,readHyDispRFLXmlAction,readHyDispRFLXmlService,getZoomLevelAction,getZoomLevelService,querySubStreamAction,querySubStreamService,querySubStreamDao,getSubStreamRFLAction,getIsoLinePicAction,getIsoLinePicService,getIsoLinePicDao]; root of factory hierarchy
2013-09-24 16:56:40 [org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:324)]-[ERROR] Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@814013] to prepare test instance [loginTest@195d4fe]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'weatherImageAction' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.nr.sd.mob.action.WeatherImageAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
... 25 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.nr.sd.mob.action.WeatherImageAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:74)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
... 39 more
Caused by: java.lang.NullPointerException
at com.nr.sd.mob.service.impl.WeatherImageServiceImpl.readImageXML(WeatherImageServiceImpl.java:261)
at com.nr.sd.mob.service.impl.WeatherImageServiceImpl.initFormate(WeatherImageServiceImpl.java:83)
at com.nr.sd.mob.service.impl.WeatherImageServiceImpl.(WeatherImageServiceImpl.java:36)
at com.nr.sd.mob.action.WeatherImageAction.(WeatherImageAction.java:26)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
... 41 more

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • zyn010101 2013-09-24 17:09
    最佳回答

    at com.nr.sd.mob.service.impl.WeatherImageServiceImpl.readImageXML(WeatherImageServiceImpl.java:261)
    at com.nr.sd.mob.service.impl.WeatherImageServiceImpl.initFormate(WeatherImageServiceImpl.java:83)
    这两行代码报空指针异常了,在WeatherImageServiceImpl.java的initFormate方法中打断点,用debug as ---junit test运行,单步调试下

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题