Results 1 to 4 of 4

Thread: Autowiring dependencies in a TestNg Test Fixure

  1. #1
    Join Date
    May 2007
    Location
    Brisbane, Australia
    Posts
    97

    Default Autowiring dependencies in a TestNg Test Fixure

    Does this stacktrace make sense? The first line says my.dao.impl.JdbcDao has been created and cached, but it can't inject the DAO into the test instance further down. Makes no sense?

    Code:
    2009-02-05 11:59:56,714 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Creating instance of bean 'my.dao.impl.JdbcDao#0'
    2009-02-05 11:59:56,752 DEBUG [org.springframework.beans.factory.annotation.InjectionMetadata] - Found injected method on class [my.dao.impl.JdbcDao]: AutowiredMethodElement for public void my.dao.impl.JdbcDao.setDataSource(javax.sql.DataSource)
    2009-02-05 11:59:56,753 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Eagerly caching bean 'my.dao.impl.JdbcDao#0' to allow for resolving potential circular references
    2009-02-05 11:59:56,753 DEBUG [org.springframework.beans.factory.annotation.InjectionMetadata] - Processing injected method of bean 'my.dao.impl.JdbcDao#0': AutowiredMethodElement for public void my.dao.impl.JdbcDao.setDataSource(javax.sql.DataSource)
    2009-02-05 11:59:56,757 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'dataSource'
    2009-02-05 11:59:56,757 DEBUG [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - Autowiring by type from bean name 'my.dao.impl.JdbcDao#0' to bean named 'dataSource'
    2009-02-05 11:59:56,773 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
    2009-02-05 11:59:56,798 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Finished creating instance of bean 'my.dao.impl.JdbcDao#0'
    2009-02-05 11:59:56,809 DEBUG [org.springframework.beans.factory.annotation.InjectionMetadata] - Found injected method on class [my.dao.impl.JdbcDaoTest]: AutowiredMethodElement for public void my.dao.impl.JdbcDaoTest.setDataSource(javax.sql.DataSource)
    2009-02-05 11:59:56,810 DEBUG [org.springframework.beans.factory.annotation.InjectionMetadata] - Found injected method on class [my.dao.impl.JdbcDaoTest]: AutowiredMethodElement for public void my.dao.impl.JdbcDaoTest.setDao(my.dao.impl.JdbcDao)
    2009-02-05 11:59:56,811 DEBUG [org.springframework.beans.factory.annotation.InjectionMetadata] - Processing injected method of bean 'my.dao.impl.JdbcDaoTest': AutowiredMethodElement for public void my.dao.impl.JdbcDaoTest.setDataSource(javax.sql.DataSource)
    2009-02-05 11:59:56,811 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'dataSource'
    2009-02-05 11:59:56,811 DEBUG [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - Autowiring by type from bean name 'my.dao.impl.JdbcDaoTest' to bean named 'dataSource'
    2009-02-05 11:59:56,960 DEBUG [org.springframework.beans.factory.annotation.InjectionMetadata] - Processing injected method of bean 'my.dao.impl.JdbcDaoTest': AutowiredMethodElement for public void my.dao.impl.JdbcDaoTest.setDao(my.dao.impl.JdbcDao)
    2009-02-05 11:59:56,962 ERROR [org.springframework.test.context.TestContextManager] - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@a93a16] to prepare test instance [my.dao.impl.JdbcDaoTest@5e9d7a]
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'my.dao.impl.JdbcDaoTest': Autowiring of methods failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void my.dao.impl.JdbcDaoTest.setDao(my.dao.impl.JdbcDao); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [my.dao.impl.JdbcDao] is defined: Unsatisfied dependency of type [class my.dao.impl.JdbcDao]: expected at least 1 matching bean
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:256)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:998)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
    	at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:117)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:585)
    It's obviously found (the only instance of ) the bean and instantiated it, so what's the go here?

    (applicationContext in case it helps:
    Code:
    <?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:p="http://www.springframework.org/schema/p"
    	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-2.5.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    	<context:annotation-config/>
    	<tx:annotation-driven/>
    	<context:property-placeholder location="classpath:jdbc.properties"/>
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="oracle.jdbc.driver.OracleDriver"
    		p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}"/>
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/>
    	<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<constructor-arg ref="dataSource"/>
    	</bean>
    	<bean class="my.dao.impl.JdbcDao" />
    </beans>

    I've tried method and field injection, with and without bean names and Qualifiers, and I'm stumped.

  2. #2
    Join Date
    May 2007
    Location
    Brisbane, Australia
    Posts
    97

    Default

    Ooh, weird. If I change the test fixture to expect the Interface of the DAO rather than the implementation, it works.

    Is this a bug, or a feature?

    I might go start using autowiring by name if this is the case. There will be more than one DAO implementations, and and thanks to some backend dodginess, I can't use the same test fixture for both implementations

  3. #3
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

    "Found injected method on class [my.dao.impl.JdbcDaoTest]: AutowiredMethodElement for public void" looks odd to me. Are you using either @Autowired on a field or a setWhatever() method?

  4. #4
    Join Date
    May 2007
    Location
    Brisbane, Australia
    Posts
    97

    Default

    Tried both. Same diff.

    Ultimately, I ended up up declaring <aop:config proxy-target-class="true"/>, and that seems to do the trick, but it's still strange.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •