Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 原因:org.springframework.beans.factory.unsatifiedDependencyException:创建名为'的bean时出错;myAppDao';:_Java_Spring_Junit - Fatal编程技术网

Java 原因:org.springframework.beans.factory.unsatifiedDependencyException:创建名为'的bean时出错;myAppDao';:

Java 原因:org.springframework.beans.factory.unsatifiedDependencyException:创建名为'的bean时出错;myAppDao';:,java,spring,junit,Java,Spring,Junit,我有一个dao类,它依赖于另一个实用程序类AuditStore package myapp; @Repository public class MyAppHibernateDao { @Autowired public void setAuditStore(AuditStore auditStore) { ConnectorLoggingHelper.setAuditStore(auditStore); } } AuditStore.java pac

我有一个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>
AuditStore
是一个非常复杂的对象,我不使用此类功能进行测试(我需要
null
或此类的模拟)。有没有什么方法可以避免创建一个用xml定义的
AuditStore
bean,并使其正常工作

我知道使
@Autowired(required=false)
可以工作,这将改变测试的应用程序代码,因此我正在寻找其他选项


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

如果可能的话,你应该考虑移动到注释驱动的配置,并使用诸如<代码> @注册表< /代码>之类的东西。但是,假设您需要坚持问题中概述的方法,您可以通过几种方式在

spring myapp db connector test.xml
中定义
MyAppHibernateDao
的模拟实例:

  • 使用。当然
  • 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
,并在测试/设置方法中设置期望值等。

您已经回答了自己的问题。。。使用模拟。如何用xml定义AuditStore类的这个模拟对象?这个想法是模拟AuditStore而不是MyAppHibernateDao,但是这种方法很有效,非常感谢。
<bean id="myAppDao" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="myapp.MyAppHibernateDao"/>
</bean>