Correct post #45: Flyway and Liquibase DO run under @DataJpaTest by default

The previous commit's FlywayDoesNotRunUnderPlainDataJpaTestTest and
FlywayRunsWhenExplicitlyImportedTest rested on a false premise: that none of
@DataJpaTest's four documented meta-annotations import FlywayAutoConfiguration,
so migrations never run under the slice. That reading of the four .imports
manifests is accurate but incomplete. @AutoConfigureJdbc is itself
meta-annotated with @AutoConfigureDataSourceInitialization (new in Boot 4.0),
and Spring's ImportAutoConfigurationImportSelector walks the full
meta-annotation tree transitively. That fifth annotation's own .imports
resource is loaded via ClassLoader.getResources(), which merges same-named
files from every jar on the classpath -- spring-boot-jdbc-test,
spring-boot-flyway and spring-boot-liquibase each ship one at the identical
path, contributing DataSourceInitializationAutoConfiguration,
FlywayAutoConfiguration and LiquibaseAutoConfiguration respectively.

Replaced the two wrong-premise test classes with FlywayRunsByDefaultTest
(proves the bean is present and the migration actually ran) and
ExplicitFlywayImportIsRedundantTest (proves the old "add the import yourself"
advice is harmless but unnecessary on Boot 4.1). Added javap/unzip evidence
for the new mechanism to docs/output, re-captured the full test run (9 tests,
1 clean skip), corrected README's test table, and closed a second,
unrelated gap: the unzip -l listing quoted in the @ServiceConnection section
had no matching captured transcript -- it does now.
This commit is contained in:
Claude
2026-10-08 12:35:04 +00:00
parent 97f946136b
commit 4370ac7fdf
9 changed files with 298 additions and 188 deletions
+6 -3
View File
@@ -47,9 +47,12 @@
<!-- flyway-core on the classpath is NOT enough to get FlywayAutoConfiguration — that class
lives in its own starter, a further instance of Boot 4's per-technology module split
(confirmed by comparing against the db-migrations-flyway-liquibase module, which needs the
exact same starter). Test scope only: this article's main app never runs Flyway itself, the
two test classes that explicitly re-import FlywayAutoConfiguration are what need the class
on the classpath at all. -->
exact same starter). This is what actually makes Flyway run under @DataJpaTest by default
in this module: @AutoConfigureJdbc's @AutoConfigureDataSourceInitialization meta-annotation
merges a META-INF/spring/...AutoConfigureDataSourceInitialization.imports file from every
jar that ships one, and spring-boot-flyway (pulled in by this starter) is one of them —
confirmed with javap and unzip, see FlywayRunsByDefaultTest's Javadoc. Test scope only:
this article's main app never runs Flyway itself outside the test slice. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-flyway</artifactId>