首页

解决Spring的JUnit4单元测试报"org.springframework.beans.factory.BeanCreationException: Erro..NoSuchBeanDefinitionException: No qualifying bean o.."错误异常问题

标签:BeanCreationException,NoSuchBeanDefinitionException,springframework,AbstractJUnit4SpringContextTests,junit4,单元测试     发布时间:2018-12-10   

一、异常描述

基于spring-test的org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests进行项目单元测试时,运行调试Junit Test代码报"org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.test.TestBase':  ..org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.test.CommonService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}..Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of ..",详情错误日志如下所示>>

十二月 13, 2018 1:44:49 上午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners@b@信息: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@1689405, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@cdf3c2, org.springframework.test.context.support.DirtiesContextTestExecutionListener@13e5454]@b@十二月 13, 2018 1:44:50 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions@b@信息: Loading XML bean definitions from URL [file:/C:/xwood_net/project/other-project/SpringMvc4/WebRoot/WEB-INF/classes/springContext.xml]@b@十二月 13, 2018 1:44:50 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh@b@信息: Refreshing org.springframework.context.support.GenericApplicationContext@12eec98: startup date [Thu Dec 13 01:44:50 CST 2018]; root of context hierarchy@b@十二月 13, 2018 1:44:50 上午 org.springframework.test.context.TestContextManager prepareTestInstance@b@严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@cdf3c2] to prepare test instance [com.test.TestBase@a68e82]@b@org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.test.TestBase': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.test.CommonService com.test.TestBase.commonService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.test.CommonService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}@b@	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)@b@	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)@b@	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)@b@	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)@b@	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)@b@	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)@b@	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)@b@	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:252)@b@	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)@b@	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:254)@b@	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217)@b@	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)@b@	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)@b@	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)@b@	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)@b@	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)@b@	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)@b@	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)@b@	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)@b@	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)@b@	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)@b@	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)@b@	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)@b@	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)@b@	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)@b@	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)@b@	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)@b@Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.test.CommonService com.test.TestBase.commonService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.test.CommonService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}@b@	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)@b@	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)@b@	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)@b@	... 26 more@b@Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.test.CommonService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}@b@	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)@b@	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)@b@	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)@b@	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)@b@	... 28 more

二、解决方法

因为项目spring的xml配置文件中没有加入”<context:component-scan base-package="com"></context:component-scan>“导致(完整配置过程及相关项目参考其他文章页

<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" @b@	xmlns:mvc="http://www.springframework.org/schema/mvc" @b@	xmlns:aop="http://www.springframework.org/schema/aop" @b@	xmlns:context="http://www.springframework.org/schema/context"@b@	xsi:schemaLocation="http://www.springframework.org/schema/beans@b@			http://www.springframework.org/schema/beans/spring-beans-4.1.xsd@b@			http://www.springframework.org/schema/context@b@			http://www.springframework.org/schema/context/spring-context-4.1.xsd">@b@@b@	<context:annotation-config />	@b@	<context:component-scan base-package="com"></context:component-scan>@b@@b@</beans>
<<热门下载>>