简体   繁体   中英

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myAppDao':

I have a dao class which has a dependency on another utility class AuditStore .

package myapp;
@Repository
public class MyAppHibernateDao {

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

}

The AuditStore.java

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

Now I want to write integration test for the dao class which don't cover functionalities of '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
    }
}

and my xml config file is

<!--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>

Whe I run this I am getting following errors.

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: {}

The AuditStore is a very complex object and I don't use this classes functionality for testing (I need a null or mock of this class). Is there any way I can avoid creating a bean of AuditStore defined in xml and make things work.

I know that making @Autowired(required = false) would work, that would make change in the application code for test, so I am looking for other options.

Please help if there is any alternative.

If possible you should consider moving to annotation driven configuration and use something like @InjectMock . However, assuming you are required to stick with the approach you outlined in your question, you can define a mock instance of MyAppHibernateDao in spring-myapp-db-connector-test.xml in several ways:

  • Use a factory bean
  • Use Springockito . And, of course,
  • Declare the myAppDao bean in spring-myapp-db-connector-test.xml as follows:

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

You can then @Autowire MyAppHibernateDao into MyAppHibernateDaoIntegrationTest and set expectations etc on it in your test/setup methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM