DBUnit load dataset and rollback

2.1k views Asked by At

I'm having troubles setting this to work. Basically I want when a tests finishes running for the database to be in the exact same state as before. This happens when using Spring/Hibernate managed sessions and connections, but not for DBUnit. I've tried lots of things and at this point I was doing something like wrapping the shared datasource in TransactionAwareDataSourceProxy and executing the load dataset manually instead of using @DatabaseSetup.

this.databaseConnection = new DatabaseConnection(dataSource.getConnection());
IDataSet xmlFileDataSet = new FlatXmlDataSetBuilder().build(getClass().getResourceAsStream("/dataset.xml"));
DatabaseOperation.REFRESH.execute(databaseConnection, xmlFileDataSet);

And it simply doesn't work. I've checked the dataSource object and it's a TransactionAwareDataSourceProxy instance so everything should be in place. All the data in the dataset is committed and persisted and all the data added/modified inside the spring managed session is not.

Does anyone have a clue what I might be missing or any did this before and ran into the same troubles?

Current code (tried with and without TransactionAwareDataSourceProxy).

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {
        RecipeManagementITConfig.class,
        SpringJpaTestConfig.class,
        DatabaseITConfig.class
},
        initializers = {PleaseWork.class})
@TestExecutionListeners({
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class,
        DbUnitTestExecutionListener.class
})
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class DefaultIngredientServiceIT {

@Autowired
protected DataSource dataSource;


@Before
public void init() throws Exception {
    System.out.println("> " + dataSource); // org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy@d4ce346
    dbTester = new DataSourceDatabaseTester(dataSource);
    dbTester.setDataSet(getDataSet());
    dbTester.setSetUpOperation(DatabaseOperation.REFRESH);
    dbTester.onSetup();
}

@After
public void destroy() throws Exception {
    dbTester.onTearDown();
}

private IDataSet getDataSet() throws Exception {
    return new FlatXmlDataSetBuilder().build(getClass().getResourceAsStream("/dataset.xml"));
}

@Transactional
@Test
public void testDeletionOfIngredients() {
   (...)
}

}

1

There are 1 answers

7
Vlad Mihalcea On

Try using a DataSourceDatabaseTester like this:

public class MyTestCase {  

   @Autowired
   private DataSource dataSource;  

   private IDatabaseTester dbTester;  

    @Before
    public void init() throws Exception {  
      dbTester = new DataSourceDatabaseTester( getDataSource() );  
      dbTester.setDataSet( getDataSet() );  
      dbTester.onSetUp();  
    }  

    @Destroy
    public void destroy() throws Exception {  
      dbTester.onTearDown();  
    }  

    private IDataSet getDataSet() throws Exception {  
      return new FlatXmlDataSet(new FileInputStream("dataset.xml"));  
    } 
}