I'm implementing an integration test suite in a Spring Boot project. My project is using Flyway for database migration.
My test class looks similar to this:
@SpringBootTest
@AutoConfigureMockMvc
@Testcontainers
internal class MyIntegrationTest {
@Container
private val postgres = postgresContainer
@Autowired
private lateinit var mockMvc: MockMvc
// ... several test cases
}
Spring Boot runs Flyway migration at application context startup. The problem is that @SpringBootTest starts application context only once, so my DB is initialized only before all tests and not before each test.
I also tried injecting a Flyway field in my test class:
@Autowired
private lateinit var flyway: Flyway
@BeforeEach
fun setup() {
flyway.migrate()
}
but there is No qualifying bean of type 'org.flywaydb.core.Flyway' available.
So, which is the right way to trigger migration before each test?
NOTES:
- (PostgreSQL) test container is correctly created and started for each single test.
- I'm using this config in application.properties:
spring.flyway.enabled=false