PDA

View Full Version : TestingError: UnsatisfiedDependencyException



JoeGranada
Jul 5th, 2005, 11:00 AM
Hello,
I am quite new to Spring and think its a real cool framework, but i encountered some problems during testing my Services:

I've made a general BaseClass extending "AbstractTransactionalSpringContextTests" and overwrote the method getConfigLocations with my SpringConfig.xml. This worked fine for DAOSupport, aber but now i have this UnsatisfiedDependencyException i can not understand.

Full Stack Trace:
org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'com.gerig.app.test.services.AddressServiceTest' defined in null:
Unsatisfied dependency expressed through bean property 'addressService': There are 2 beans of type [class com.gerig.app.services.base.AddressService] for autowire by type.
There should have been 1 to be able to autowire property 'addressService' of bean 'com.gerig.app.test.services.AddressServiceTest'.


at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireByType(Abstract AutowireCapableBeanFactory.java:800)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:720)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireBeanProperties( AbstractAutowireCapableBeanFactory.java:188)
at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.setUp(AbstractDependencyInjec tionSpringContextTests.java:141)
at junit.framework.TestCase.runBare(TestCase.java:125 )
at junit.framework.TestResult$1.protect(TestResult.ja va:106)
at junit.framework.TestResult.runProtected(TestResult .java:124)
at junit.framework.TestResult.run(TestResult.java:109 )
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)



But i only have one bean called addressService:
<bean id="addressService" parent="transactionInterceptorTemplate">
<property name="target">
<ref bean="addressServiceTarget"/>
</property>
</bean>

the AddressServiceTarget:

<bean id="addressServiceTarget" class="com.gerig.app.services.base.AddressService">
<property name="addressDAO">
<ref bean="addressDAO"/>
</property>
<property name="lovService">
<ref bean="lovService"/>
</property>
<property name="zipCode2CityService">
<ref bean="zipCode2CityService"/>
</property>
</bean>

I donīt know the Problem. Is there any way to solve this Probem??

Thank you!

Rod Johnson
Jul 6th, 2005, 10:58 AM
Make the target an inner bean. That is, make the contents of the "target" property value a bean definition rather than a reference.

Rgds
Rod

timrobertson100
Jan 18th, 2006, 05:09 AM
So I changed it to:


<property name="target">
<bean class="org.gbif.col2005.service.impl.TaxaManagerImpl">
<property name="taxaDAO" ref="taxaDAO"/>
</bean>
</property>


And it got rid of the error - hope this helps someone else

Rod Johnson
Jan 22nd, 2006, 07:11 PM
Using "autoproxying" approaches--which will become more common with Spring 2.0--also avoids the duplicate bean error.

The old practice XX and XXTarget should usually be avoided in favour of the inner bean, with Spring 1.2.x also.

xstevens
Feb 3rd, 2006, 12:26 PM
I am having a similar problem but moving the the "dataSource" bean into data access classes doesn't fix the problem for me. Any ideas?

Here is the error:
org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'com.mycompany.user.dao.UserDaoTest' defined in null: Unsatisfied dependency expressed through bean property 'userDao': set this property value or disable dependency checking for this bean
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.checkDependencies(Abstr actAutowireCapableBeanFactory.java:962)

Here is the test bean definitions:
<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>

<bean id="userDao" class="com.mycompany.user.dao.jdbc.JdbcUserDao">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="userGroupDao"><ref bean="userGroupDao"/></property>
</bean>

<bean id="userDaoTest" class="com.mycompany.user.dao.UserDaoTest">
<property name="userDao"><ref bean="userDao"/></property>
</bean>

<bean id="userGroupDao" class="com.mycompany.user.dao.jdbc.JdbcUserGroupDao">
<property name="dataSource"><ref local="dataSource"/></property>
</bean>
</beans>

Oreoferret
Aug 22nd, 2006, 09:16 AM
Is the use of ref="target" only for Spring 2.0?

<bean id="jobRequestDAO"
class="com.spinsys.agent.job.dao.JobRequestDAO">
<property name="dataSource"> <ref bean="avianDS"/></property>
</bean>

that works for me in general.. but once I switched to trying to write a unit test with extends AbstractTransactionalDataSourceSpringContextTests. .. nothing works.

I get the exception mentioned at the start of this thread (that is can't find a bean to autowire but it found 4 beans etc).

I have 2 xml files application-DS.xml and application-DAO.xml.

I tried to ref="avianDS" without adding 'bean' and that fails the sax parser.

any hints?

Oreoferret
Aug 22nd, 2006, 09:28 AM
So.. i got it to work.. but the only means I could do so was to put the dataSource bean, as well as the daos, as well as my transaction manager.. *all in one file*.

Is there any way to have these work if my dataSource's are in an applicationDS.xml, and my DAOs are in an applicationDAO.xml. If I seperate the files, I get the 'found too many beans to autowire' problem (it seems to multiple in 2's).

Thanks