Peculiar reset-behavior with H2/Spring in Tests

136 Views Asked by At

We have a microservice based on Spring Boot 2.7.6. For testing we employ dbunit/junit4 and a h2 database. We have some integrationtests which establish a database-baseline via a schema.sql and then some dbunit-xml-magic. Those tests are marked with @DirtiesContext.

My understanding of @DirtiesContext is, that after the test both the database and the Spring context should be discarded and rebuilt. Up to now this has worked well.

(Almost) out of the blue those tests fail. The error message is java.lang.IllegalStateException: Failed to load ApplicationContext with the cause, that a sequence cannot be created, because it already exists. This sequence is being created by the schema.sql. It is my understanding that the error message implies the databases are not discarded between the tests.

The current changes on the branch are purely business driven. While some classes have changed their packages (which seems to have some effect on the ordering during initialization by Spring), no changes on the testframework itself or the like were made.

I can't isolate the change which triggered the behavior but some experimentation showed, downgrading the h2 database from 2.1.212 to 1.4.199 relaxes the problem (fewer tests fail) and downgrading to 1.4.190 resolves the issue.

The question now is, is this some form of bug? If so, some bug in h2 2.1 oder in 1.4? Is my understanding incorrect, that @DirtiesContext should clean out the database?

0

There are 0 best solutions below