Results 1 to 8 of 8

Thread: @Transactional is ignored in Junit4 test

  1. #1

    Default @Transactional is ignored in Junit4 test

    Hi,

    I'm trying to convert my test classes to use the @Transactional annotation instead of extending AbstractTransactionalJUnit4SpringContextTests and using @NotTransactional. This is the recommended approach, according to SPR-6043.

    But, for some reason, I cannot get the @Transactional annotation to do anything! When I put a breakpoint in the test method annotated by @Transactional, I cannot see TransactionInterceptor in the stack trace, as I expect to. In fact, I do not see the test class is proxied at all.

    What am I doing wrong?

    Here’s my application context XML:

    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: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">
    
    	<tx:annotation-driven transaction-manager="transactionManager" />
    
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="default">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    	
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" lazy-init="default">
    
    		<!-- connection configuration -->
    		<property name="driverClassName" value="org.postgresql.Driver" />
    		<property name="url" value="jdbc:postgresql://localhost:5432/mydb" />
    		<property name="username" value="user" />
    		<property name="password" value="pass" />
    
    	</bean>
    
     	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" lazy-init="default">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
    				<prop key="hibernate.show_sql">false</prop>
    				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
    				<prop key="hibernate.cache.use_second_level_cache">false</prop>
    				<prop key="hibernate.cache.use_query_cache">false</prop>
    			</props>
    		</property>
    	</bean>
    	
    </beans>
    And here’s a simplified test class, based on the example from http://static.springsource.org/sprin...testcontext-tx:

    Code:
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.transaction.TransactionConfiguration;
    import org.springframework.transaction.annotation.Transactional;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    @TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
    public class FictitiousTransactionalTest {
    
        @Test
        @Transactional
        public void modifyDatabaseWithinTransaction() {
        	System.out.println();
        }
        
    }
    Any idea?

    Thanks,
    Moshe

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Your testcase does nothing, so not sure what it should do at all..
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Hi,

    It is just a simplified version of the test, to filter out noise. I run it with debug breakpoint on the system.out.println() line, and I check the stack trace. I do not see the TransactionInterceptor there, as I normally do in methods annotated with @Transactional.

    Thanks,
    Moshe.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    And what an earth has your testcase to do with the proxying of your service/dao.. You testcase isn't a spring bean and as such doesn't get proxied...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    I read the documentation in http://static.springsource.org/sprin...testcontext-tx - it doesn't say anything about making the test case a bean. The example I gave is based on the example given in the documentation.

    but perhaps I miss something: how is the @Transactional annotation supposed to work in test classes?

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    but perhaps I miss something: how is the @Transactional annotation supposed to work in test classes?
    That is the point is was trying to make...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Nov 2005
    Posts
    113

    Default

    Quote Originally Posted by mosheb@nexperience.com View Post
    how is the @Transactional annotation supposed to work in test classes?
    @Transactional in test classes only works in the context of the transactional service/dao you're testing. The basic gist of it is that with @Transactional, the test framework will start a transaction on test start, but roll it back on test completion. This is all done by the TestRunner, so there's no need for a TransactionInterceptor or anything like that.

    Hope this helps
    - Don

  8. #8

    Thumbs up

    Quote Originally Posted by dbrinker View Post
    @Transactional in test classes only works in the context of the transactional service/dao you're testing. The basic gist of it is that with @Transactional, the test framework will start a transaction on test start, but roll it back on test completion. This is all done by the TestRunner, so there's no need for a TransactionInterceptor or anything like that.

    Hope this helps
    - Don
    Don, thanks for clarifying this. Your explanation really helped

Posting Permissions

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