How to inspect database when running with spring-db-unit

468 views Asked by At

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 @DatabaseSetupannotation 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 :

  1. com.github.ppodgorsek:spring-test-dbunit-core:5.2.0
  2. org.dbunit:dbunit:2.6.0
  3. junit:junit:4.12
  4. org.springframework.boot:spring-boot-starter-parent:1.3.8.RELEASE
  5. 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!

1

There are 1 answers

0
dube On BEST ANSWER

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:

By default, test transactions will be automatically rolled back after completion of the test; however, transactional commit and rollback behavior can be configured declaratively via the @Rollback annotation at the class level and at the method level.

The solution is to put @org.springframework.test.annotation.Rollback(false) on the test class or method.