当前位置: 首页 > 知识库问答 >
问题:

@使用JUnit自动连线失败

陆耀
2023-03-14

我真的需要帮助。我使用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

共有2个答案

郁吉星
2023-03-14

查看您的属性id。它应该是applicationContext service.xml中的manageTargetServiceImpl

把它改成这样:

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

尹昀
2023-03-14

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

Biju-Kunjummen

 类似资料:
  • 我正在努力学习一本书名为《SpringMVC初学者指南》的书,我一直在努力创建存储库对象。我不断地得到一个BeanCreationException。不知道我还错过了什么。我想知道是否有人能帮我解决这个问题。 请在下面找到我的代码。谢谢 BeanCreationException XML文件: ProductCrontroller: 产品存储库: InMemoryProductRepository

  • 如何测试使用JdbcTemplate和SimpleJDBCall的repository类,如下所示 这就是我尝试过的 下面是测试代码和存储库代码,我使用的是Spring Boot最新版本junit5和mockito。我已经尝试了下面的解决方案,但可以让它工作 带Spring JdbcTemplate的SimpleJDBCall的Mockito 下面是存储库代码@repository public

  • 我在Spring+SpringMVC+Hibernate+MySQL web应用程序中的Spring配置有一个问题。Spring无法创建我在Service类中宣布的bean。 下面是Controller类 应用程序-上下文 最后是我的StackTrace 原因:org.SpringFramework.Beans.Factory.BeanCreationException:无法自动连接字段:priv

  • 被异常卡住,下面是日志: org.springframework.beans.factory.BeanCreation异常:创建名为扬声器的bean时出错:注入自动生成的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreation异常:无法自动连接方法:公共最终无效org.mybatis.spring.support.SqlSessionDao

  • 我找不到任何原因来解释为什么每个自动连接的bean都不是通过代理自动连接的。我知道,因为事务注释不起作用,我在eclipse中调试时检查了自动连线组件。当然,每个组件都实现了一些接口,我使用了与接口相关的注释。我只有一种aop配置: 我将JPA与hibernate、spring mvc、spring webflow、spring security和spring数据一起使用。扩展组织的接口。spri

  • 我想知道以下解决方案之间的区别是什么,为什么使用解决方案2?有什么好处吗? 解决方案一: 解决方案2: