2

真的需要帮助。我使用 JUnit(4.6) 和 Spring(3.0.5) 进行单元测试。当我尝试在我的测试类中自动装配服务对象时,我得到了 NoSuchBeanDefinitionException。

JUnit 代码:

package com.aaa.service.impl;

@ContextConfiguration(locations = { "classpath:/spring/applicationContext.xml",
    "classpath:/spring/applicationContext-aop.xml",
    "classpath:/spring/applicationContext-dao.xml",
    "classpath:/spring/applicationContext-service.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class TestManageTargetServiceImpl {

    @Autowired
    ManageTargetServiceImpl manageTargetServiceImpl;

    @Test
    public void testQueryFinancialMonthList() {
        List<Map<String, Object>> value = new ArrayList<Map<String, Object>>();
        ManageTargetDao manageTargetDao = mock(ManageTargetDao.class);
        when(manageTargetDao.queryFinancialMonthList()).thenReturn(value);
        manageTargetServiceImpl.setManageTargetDao(manageTargetDao);
        // I hope it return null
        assertNull(manageTargetServiceImpl.queryFinancialMonthList());
    }
}

applicationContext-service.xml 代码:

<context:annotation-config />

<bean id="manageTargetService" class="com.aaa.service.impl.ManageTargetServiceImpl">
    <property name="manageTargetDao" ref="manageTargetDao"></property>
</bean>

错误轨迹:

00:51:28,625 ERROR main context.TestContextManager:324 - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@882dfc] to prepare test instance [com.aaa.service.impl.TestManageTargetServiceImpl@ce2db0]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.aaa.service.impl.TestManageTargetServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.aaa.service.impl.ManageTargetServiceImpl com.aaa.service.impl.TestManageTargetServiceImpl.manageTargetServiceImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.aaa.service.impl.ManageTargetServiceImpl] 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)}

一些日志:

00:51:26,828 DEBUG main context.TestContextManager:282 - beforeTestClass(): class [class com.aaa.service.impl.TestManageTargetServiceImpl]
00:51:26,828 DEBUG main annotation.ProfileValueUtils:68 - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.aaa.service.impl.TestManageTargetServiceImpl]
00:51:26,828 DEBUG main annotation.ProfileValueUtils:80 - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.aaa.service.impl.TestManageTargetServiceImpl]
00:51:26,828 DEBUG main context.TestContextManager:315 - prepareTestInstance(): instance [com.aaa.service.impl.TestManageTargetServiceImpl@ce2db0]
00:51:26,843 DEBUG main support.DependencyInjectionTestExecutionListener:73 - Performing dependency injection for test context [[TestContext@e0eb3f testClass = TestManageTargetServiceImpl, locations = array<String>['classpath:/spring/applicationContext.xml', 'classpath:/spring/applicationContext-aop.xml', 'classpath:/spring/applicationContext-dao.xml', 'classpath:/spring/applicationContext-service.xml'], testInstance = com.aaa.service.impl.TestManageTargetServiceImpl@ce2db0, testMethod = [null], testException = [null]]].
00:51:26,843 DEBUG main support.AbstractGenericContextLoader:75 - Loading ApplicationContext for locations [classpath:/spring/applicationContext.xml,classpath:/spring/applicationContext-aop.xml,classpath:/spring/applicationContext-dao.xml,classpath:/spring/applicationContext-service.xml].
00:51:26,968  INFO main xml.XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [spring/applicationContext.xml]
00:51:27,187  INFO main xml.XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [spring/applicationContext-aop.xml]
00:51:27,312  INFO main xml.XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [spring/applicationContext-dao.xml]
00:51:27,421  INFO main xml.XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [spring/applicationContext-service.xml]
00:51:27,453  INFO main support.GenericApplicationContext:456 - Refreshing org.springframework.context.support.GenericApplicationContext@16c14e7: startup date [Tue Jan 01 00:51:27 NZDT 2013]; root of context hierarchy
00:51:27,718  INFO main config.PropertyPlaceholderConfigurer:177 - Loading properties file from class path resource [jdbc.properties]
00:51:27,796  INFO main support.DefaultListableBeanFactory:555 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2938d8: defining beans [loggingAop,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,service,loginDao,manageTargetDao,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,txManager,txAdvice,txDAO,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,loginService,manageTargetService]; root of factory hierarchy
4

2 回答 2

0

看看你的property id. 它应该manageTargetServiceImpl在你的applicationContext-service.xml

将其更改为:

<bean id="manageTargetServiceImpl" class="com.aaa.service.impl.ManageTargetServiceImpl">
    <property name="manageTargetDao" ref="manageTargetDao"></property>
</bean>
于 2012-12-31T12:42:38.467 回答
0

在这里猜测 - 但鉴于类类型 ManageTargetServiceImpl 的注入失败,我认为原因是正在为 ManageTargetServiceImpl 创建代理,因此 bean 的类型将不再是 ManageTargetServiceImpl 而是该类的接口。

Biju Kunjummen

于 2012-12-31T23:30:47.270 回答