Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Transactional in unit test prevents @PostLoad from being invoked

I have the following test:

public class Book
{
     public boolean postLoadInvoked;
     @PostLoad
     private void onPostLoad()
     {
         postLoadInvoked = true;
     }
}

public class MyIntegrationTest extends AbstractIntegrationTest
{
     @Autowired
     private BookDAO bookDAO;

     @Test
     public void loadBooks()
     {
          Book book = bookDAO.findOne(...);
          assertTrue(book.postLoadInvoked);
     }  
}

This test passes as-is, but if I add the @Transactional annotations to the test class, it fails:

@Transactional
@TransactionConfiguration(defaultRollback=true)
public class MyIntegrationTest extends AbstractIntegrationTest

Why does configuring the test with @Transactional affect the JPA callback methods?

EDIT

The DAO is just a Spring Data repository, so has no logic:

public interface BookDAO extends
   JpaRepository<Book, Long>, 
   QueryDslPredicateExecutor<Book> {}

The transaction manager also has a standard configuration:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>com.mangofactory.example</value>
        </list>
    </property>
    <property name="persistenceUnitName" value="spring-test" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
        </bean>
    </property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
</bean>
like image 603
Marty Pitt Avatar asked Aug 17 '12 04:08

Marty Pitt


1 Answers

I'm not sure what the spring version is that you are using or if AbstractIntegrationTest is extending something, but one thing to check is that in your hierarchy your tests should extend some abstract transactional context aware spring test class.

The classes that I'm thinking about:

org.springframework.test.AbstractTransactionalSpringContextTests
org.springframework.test.AbstractTransactionalDataSourceSpringContextTests

or some jUnit flavor (depending on the version that you are using):

org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests
org.springframework.test.context.junit38.AbstractTransactionalJUnit38SpringContextTests
like image 138
costty000 Avatar answered Oct 07 '22 03:10

costty000