Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring TransactionalDataSource tests not being rolled back as expected

 
James Adams
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic