We run our tests with spring-db-unit, the forked and maintained one. The tests run just fine and can access and modify the data. But when I was debugging some tests I realized that the database stays empty all the time! My assumption is that the runner creates a transaction and rolls it back at the end of the test instead of commiting.
How can I inspect the data or persist it in the database at the end (or anytime, really!) of a test?
The setup:
Our config is follows the suggested setup from the gibthub readme:
@TestExecutionListeners(
inheritListeners = false,
value = {
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class,
WithSecurityContextTestExecutionListener.class
})
@DatabaseSetup(value = "/testdata/dbunit/base_test_dataset.xml", type = DatabaseOperation.CLEAN_INSERT)
@SpringApplicationConfiguration(classes = {
DBUnitTestConfig.class,
})
@DbUnitConfiguration(databaseConnection = "dbUnitConnection")
public class ReviewServiceTest {
@Test
@WithUserDetails(TEST_COCKPIT_1)
public void getUserVersionReviews() throws Exception {
// when in a debug point here, the DB is empty.
}
}
The database is PostgreSQL 11.4. The DBUnitTestConfig
class above just configures the PostgresqlDataTypeFactory
of the DatabaseConfigBean
Switching to TransactionDbUnitTestExecutionListener
instead of TransactionalTestExecutionListener
and DbUnitTestExecutionListener
didn't help.
I tried it with DatabaseOperation.INSERT
instead of the default one and moved the @DatabaseSetup
annotation onto test methods and setUp methods, without success.
We are using the following dependencies. It's not latest and the furthest I was able upgrade for now :
- com.github.ppodgorsek:spring-test-dbunit-core:5.2.0
- org.dbunit:dbunit:2.6.0
- junit:junit:4.12
- org.springframework.boot:spring-boot-starter-parent:1.3.8.RELEASE
- OpenJDK 1.8
What confuses me that I couldn't find anyone complaining or configuration options for it, so I must be missing something obivious!
Rolling back transactions isn't spring-test-dbunit fault. It works with and without transactions. The roll back is the default behavior of the
TransactionalTestExecutionListener
.Citing the javadoc of the class:
The solution is to put
@org.springframework.test.annotation.Rollback(false)
on the test class or method.