Results 1 to 4 of 4

Thread: AbstractTransactionalJUnit4SpringContextTests with JTA

  1. #1
    Join Date
    Jan 2008
    Posts
    5

    Default AbstractTransactionalJUnit4SpringContextTests with JTA

    I'm trying to run an junit test with JTA outside of a jee container.

    I have two datasources in my XXX-context.xml

    <bean id="datasource1" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close">
    <property name="uniqueResourceName"><value>${jdbc.datasource 1}</value></property>
    <property name="xaDataSourceClassName"><value>${jdbc.driverC lassName}</value></property>
    <property name="xaDataSourceProperties">
    ...
    </property>
    ....
    </bean>

    <bean id="sessionFactory1" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean"
    p:dataSource-ref="datasource1">
    ...
    <prop key="hibernate.transaction.factory_class">org.hibe rnate.transaction.JTATransactionFactory</prop>
    <prop key="hibernate.transaction.manager_lookup_class">c om.atomikos.icatch.jta.hibernate3.TransactionManag erLookup</prop>
    </bean>

    and the same for the second data source (datasource2) and session factory.

    I'm extending the AbstractTransactionalJUnit4SpringContextTests and when I try to run it I get the following exception :

    org.springframework.beans.factory.BeanCreationExce ption: Could not autowire method: public void org.springframework.test.context.junit4.AbstractTr ansactionalJUnit4SpringContextTests.setDataSource( javax.sql.DataSource);
    nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [javax.sql.DataSource] is defined:
    expected single matching bean but found 2: [datasource1, datasource2]

    The problem looks to be that the method public void setDataSource(final DataSource dataSource) in the class AbstractTransactionalJUnit4SpringContextTests is
    annotated with @Autowired.

    Any idea about how could I solve this ?

    Thanks.

    --
    K 4 |v| 4

  2. #2

    Default

    post the test class at least with the method that don't work

  3. #3
    Join Date
    Jan 2008
    Posts
    5

    Default Test class

    Hi
    The offending test class is :

    @ContextConfiguration
    public class MyRepositoryImplTests
    extends AbstractTransactionalJUnit4SpringContextTests{

    @Test
    public void testSimpleTest(){
    ...
    }
    }

    and the exception :
    ERROR - TestContextManager:234 - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener@14492fb]
    to prepare test instance [a.b.c.MyRepositoryImplTests@10dd4c8]
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'a.b.c.MyRepositoryImplTests':
    Autowiring of methods failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire method:
    public void org.springframework.test.context.junit4.AbstractTr ansactionalJUnit4SpringContextTests.setDataSource( javax.sql.DataSource); nested exception is
    org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [javax.sql.DataSource] is defined: expected single matching
    bean but found 2: [datasource1, dataSource2] at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessPropert yValues(AutowiredAnnotationBeanPostProcessor.java: 253)

  4. #4

    Default

    Code:
    No unique bean of type [javax.sql.DataSource] is defined: expected single matching
    bean but found 2: [datasource1, dataSource2] at org.springframework.beans.factory.annotation.Autow
    you are autowiring bytype and you have 2 classes

Posting Permissions

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