I’m trying to load data for integration tests using Liquibase, but have the transaction rollback so I can use different data in other tests.
So, I have a lot of INSERTs that I wanna do inside a Spring-driven JUnit test. My changesets look like:-
changeSet
runInTransaction = “false” author = “me (generated)”
=
“1413580824558-29” >
-
insert
tableName = “some_table” >
-
column
name
“id”
valueNumeric
“10”
/>
-
< column name = “created” valueDate = “2014-10-17 10:25:55.0”
/>
-
column
name = “modified” valueDate = “2014-10-17 10:25:55.0”
/>
-
insert
>
My Spring-driven JUnit test looks like:
-
@RunWith (SpringJUnit4ClassRunner. class )
-
@ContextConfiguration (classes= {PersistenceJPAConfig.
class
})
-
@Transactional
()
-
@TestExecutionListeners ({
-
DependencyInjectionTestExecutionListener. class ,
-
TransactionalTestExecutionListener. class
-
})
-
@TransactionConfiguration (defaultRollback= true )
-
public
class DatabaseBasedIT {
I have a
method that loads a changelog via the Spring-bean:
-
protected
void loadLiquibaseChangelog(String
changelogFilename )
throws LiquibaseException {
-
SpringLiquibase springLiquibase =
new SpringLiquibase();
-
springLiquibase .setDataSource( ds );
-
springLiquibase .setChangeLog(
changelogFilename );
-
springLiquibase .setResourceLoader( resourceLoader
);
-
springLiquibase .afterPropertiesSet();
-
}
Spring says it rolled back the tx:
-
INFO 10/22/14 10:16 PM: liquibase: Successfully released change log lock
-
Oct 22, 2014 10:16:54 PM org.springframework.test.context.transaction.TransactionContext endTransaction
-
INFO: Rolled back transaction for test context [DefaultTestContext@14af2ab6 testClass = DatabaseBasedIT, testInstance =DatabaseBasedIT@2e5e130a, testMethod = onlyLoadsChangeLogFromLiquibase@DatabaseBasedIT, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@3f70f50e testClass = DatabaseBasedIT, locations = ‘{}’, classes = ‘{class PersistenceJPAConfig}’, contextInitializerClasses = ‘[]’, activeProfiles = ‘{}’, propertySourceLocations = ‘{}’, propertySourceProperties = ‘{}’, contextLoader = ‘org.springframework.test.context.support.DelegatingSmartContextLoader’, parent = [null]]].
But the data doesn't actually get rolled back because the data is still left over after the tests are done. (If I manually INSERT the data using the DataSource that Spring gives me, that gets rolled back properly.
Looking at the code for liquibase.changelog.
ChangeSet and setting some breakpoints, I can see that autocommit is set to true. Could this be the reason that the liquibase data isn’t rolled back?
P.S. I can create a small sample
project if need be, but I’m hoping it’s easy enough to
figure out with that. ;)
P.S.S. I forgot to mention that I’m trying setting runInTransaction to false because it doesn’t work with the default either.