Spring 单元测试找不到对应的bean

刚学spring,在做单元测试的时候,找不到对应的bean,但是我xml文件中已经配置好了bean,不知道是不是哪里错了。这个是目录结构
图片说明
下面是InjectionService和InjectionDAO两个类
图片说明

图片说明

下面是spring-injection.xml文件:

图片说明

下面是错误信息:
图片说明
console输出信息:
三月 31, 2017 10:02:09 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@b065c63: startup date [Fri Mar 31 10:02:09 SGT 2017]; root of context hierarchy
三月 31, 2017 10:02:09 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@47c62251: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy

7个回答

你的测试代码呢,可以贴出来看一下嘛

DanielRichard
RicardoLei 你好,测试代码放下面了,恳请指点,万分感谢。
3 年多之前 回复

忘记贴测试代码了,抱歉。。。
图片说明

classpath*:resources/spring-injection.xml 这里错了,不需要加resources @ContextConfiguration({"classpath*:spring-injection.xml"}) 这样就可以

DanielRichard
RicardoLei 回复在路上的JavaCoder-尹骑: 原因贴在下面了,万分感谢
3 年多之前 回复
qq455276333
在路上的JavaCoder-尹骑 回复Daniel7319: 把下面的原因贴出来,如果正常这样写是没有问题的
3 年多之前 回复
DanielRichard
RicardoLei 回复在路上的JavaCoder-尹骑: 错误情况贴下面了,谢谢大神
3 年多之前 回复
qq455276333
在路上的JavaCoder-尹骑 回复Daniel7319: 额,贴出来看看
3 年多之前 回复
DanielRichard
RicardoLei 严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@3b088d51] to prepare test instance [spring.learning.TestInjection@1786dec2] java.lang.IllegalStateException: Failed to load ApplicationContext
3 年多之前 回复
DanielRichard
RicardoLei 不行呀,错误的情况更多了
3 年多之前 回复

去掉resources之后,出现了下面的错误情况:
三月 31, 2017 2:00:46 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/D:/eclipse/MvnWorkSpace/spring.learning/target/test-classes/spring-injection.xml]
三月 31, 2017 2:00:46 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/D:/eclipse/MvnWorkSpace/spring.learning/target/classes/spring-injection.xml]
三月 31, 2017 2:00:46 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
信息: Overriding bean definition for bean 'injectionService': replacing [Generic bean: class [spring.learning.InjctionService]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [file:/D:/eclipse/MvnWorkSpace/spring.learning/target/test-classes/spring-injection.xml]] with [Generic bean: class [spring.learning.InjctionService]; scope=; abstract=false; lazyInit=false; autowireMode=1; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [file:/D:/eclipse/MvnWorkSpace/spring.learning/target/classes/spring-injection.xml]]
三月 31, 2017 2:00:46 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
信息: Overriding bean definition for bean 'injectionDAO': replacing [Generic bean: class [spring.learning.InjctionDAO]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [file:/D:/eclipse/MvnWorkSpace/spring.learning/target/test-classes/spring-injection.xml]] with [Generic bean: class [spring.learning.InjctionDAO]; scope=; abstract=false; lazyInit=false; autowireMode=1; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [file:/D:/eclipse/MvnWorkSpace/spring.learning/target/classes/spring-injection.xml]]
三月 31, 2017 2:00:46 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@b065c63: startup date [Fri Mar 31 14:00:46 SGT 2017]; root of context hierarchy
三月 31, 2017 2:00:46 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5649fd9b: defining beans [injectionService,injectionDAO,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
三月 31, 2017 2:00:46 下午 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5649fd9b: defining beans [injectionService,injectionDAO,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
三月 31, 2017 2:00:46 下午 org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@3b088d51] to prepare test instance [spring.learning.TestInjection@1786dec2]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
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:312)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
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.CannotLoadBeanClassException: Cannot find class [spring.learning.InjctionService] for bean with name 'injectionService' defined in URL [file:/D:/eclipse/MvnWorkSpace/spring.learning/target/classes/spring-injection.xml]; nested exception is java.lang.ClassNotFoundException: spring.learning.InjctionService
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1275)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:575)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1344)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:910)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:120)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
... 25 more
Caused by: java.lang.ClassNotFoundException: spring.learning.InjctionService
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:260)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:416)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1296)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1267)
... 37 more

Caused by: java.lang.ClassNotFoundException: spring.learning.InjctionService 报错信息是这个类找不到,你在spring配置文件中,
点击class 属性可以进入类中嘛,按住ctrl 键点鼠标左键。

DanielRichard
RicardoLei 不行,spring-injection.xml文件中那两个bean的class属性,按ctrl+单击没有反应,是不是我的目录结构有问题?
3 年多之前 回复

错误原因如下:
图片说明
图片说明

如果还有问题的话也没事,我按照你的代码写了一份,等下下班了共享给你

DanielRichard
RicardoLei 不好意思打扰你了,我讲项目导入eclipse中,发现“Autowired cannot be resolved to a type”,网上说是jdk错误,但是我自己写的项目没有这个问题,请问需要如何配置呢?谢谢
3 年多之前 回复
qq455276333
在路上的JavaCoder-尹骑 这是我刚写的例子:http://download.csdn.net/detail/qq455276333/9800666
3 年多之前 回复
DanielRichard
RicardoLei 万分感谢!我用maven新建的web项目。麻烦了
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐