gpt4 book ai didi

java - 由 : org. springframework.beans.factory.UnsatisfiedDependencyException 引起:创建名称为 'myAppDao' 的 bean 时出错:

转载 作者:行者123 更新时间:2023-12-02 12:17:27 24 4
gpt4 key购买 nike

我有一个 dao 类,它依赖于另一个实用程序类 AuditStore

package myapp;
@Repository
public class MyAppHibernateDao {

@Autowired
public void setAuditStore(AuditStore auditStore) {
ConnectorLoggingHelper.setAuditStore(auditStore);
}

}

AuditStore.java

package myapp;
@Resource
public class AuditStore {
//too many dependencies in this class including db connection
}

现在我想为 dao 类编写集成测试,该测试不涵盖“AuditStore”的功能。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:META-INF/spring-myapp-db-connector-test.xml")
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class})
public class MyAppHibernateDaoIntegrationTest {


@Test
public void test() {
//test code here
}
}

我的 xml 配置文件是

<!--spring-myapp-db-connector-test.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- enable autowiring -->

<context:annotation-config/>

<bean id="myAppDao" class="myapp.MyAppHibernateDao">

</beans>

当我运行这个时,我收到以下错误。

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myAppDao': Unsatisfied dependency expressed through method 'setAuditStore' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'myapp.AuditStore' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

AuditStore 是一个非常复杂的对象,我不使用此类功能进行测试(我需要此类的 null 或模拟)。有什么方法可以避免创建在 xml 中定义的 AuditStore bean 并使事情正常工作。

我知道制作 @Autowired(required = false) 会起作用,这会更改应用程序代码以进行测试,因此我正在寻找其他选项。

如果有其他选择,请帮忙。

最佳答案

如果可能,您应该考虑转向注释驱动配置并使用类似@InjectMock的东西。但是,假设您需要坚持问题中概述的方法,您可以在 spring-myapp-db-connector-test.xml 中定义 MyAppHibernateDao 的模拟实例> 以多种方式:

  • Use a factory bean
  • 使用Springockito 。当然,
  • spring-myapp-db-connector-test.xml 中声明 myAppDao bean,如下所示:

    <bean id="myAppDao" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="myapp.MyAppHibernateDao"/>
    </bean>

然后,您可以将 @Autowire MyAppHibernateDao 放入 MyAppHibernateDaoIntegrationTest 中,并在测试/设置方法中对其设置期望等。

关于java - 由 : org. springframework.beans.factory.UnsatisfiedDependencyException 引起:创建名称为 'myAppDao' 的 bean 时出错:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46048422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com