wood burning stoves 2.0*
The moose likes Other Application Frameworks and the fly likes Spring TransactionalDataSource tests not being rolled back as expected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring TransactionalDataSource tests not being rolled back as expected" Watch "Spring TransactionalDataSource tests not being rolled back as expected" New topic
Author

Spring TransactionalDataSource tests not being rolled back as expected

James Adams
Ranch Hand

Joined: Sep 12, 2003
Posts: 188
I have several tests which extend AbstractTransactionalDataSourceSpringContextTests and they appear to not be rolling back as expected when I run more than one of them together, as in the case of my Maven build. I get tests failing because they expect the database tables to be in a clean state when they start, and instead there is data already present, making it impossible to assert that a certain number of records are present after using DAO classes to save/delete/etc.

I have confirmed that this behavior happens using both HSQLDB and Derby embedded databases.

Can anyone suggest something I can try to find the source of this problem?

Thanks in advance for any assistance.

--James
[ December 17, 2008: Message edited by: James Adams ]
James Adams
Ranch Hand

Joined: Sep 12, 2003
Posts: 188
I just worked out my problem -- as is often the case it was caused by my own foolishness.

One of my tests which wasn't extending the Spring transactional data source test class is testing one component which contains/uses a DAO component, and this DAO isn't being mocked in the test but instead is coming from the application context, and it's writing to the database as part of the test execution. Since this test is not in a transaction which is rolled back the data remains in the database and the following tests don't pass, since they're expected empty database tables. My original premise that I was running all my DB-related tests within transactional test case classes was wrong, and once I made it so everything worked as expected.

--James
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

Originally posted by James Adams:
I just worked out my problem -- as is often the case it was caused by my own foolishness.

One of my tests which wasn't extending the Spring transactional data source test class is testing one component which contains/uses a DAO component, and this DAO isn't being mocked in the test but instead is coming from the application context, and it's writing to the database as part of the test execution. Since this test is not in a transaction which is rolled back the data remains in the database and the following tests don't pass, since they're expected empty database tables. My original premise that I was running all my DB-related tests within transactional test case classes was wrong, and once I made it so everything worked as expected.

--James




Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
wood burning stoves
 
subject: Spring TransactionalDataSource tests not being rolled back as expected