Sub topic 12
Testing migrations against a real database
This chapter assumes you can write versioned migrations, know that Spring Boot runs Flyway at startup, and can use JUnit 5 with @SpringBootTest. It also assumes Docker is available on your machine.
Why not test against H2
An in-memory database such as H2 accepts different SQL than PostgreSQL. A migration can pass against H2 and then fail in production because of identity column syntax, CREATE OR REPLACE VIEW, or a PostgreSQL-specific type. A migration test is only useful if it runs on the same database engine you deploy to.
Testcontainers solves this by starting a disposable PostgreSQL container for the test run. Spring Boot connects the application to that container automatically, so Flyway runs every migration against a real server.
Add the test dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>Spring Boot's dependency management supplies the versions. spring-boot-testcontainers requires Spring Boot 3.1 or later.
Write the test
Starting the application context triggers Flyway. The test then asks Flyway what is left to do.
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.jdbc.core.JdbcTemplate;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
@Testcontainers
class MigrationsTest {
@Container
@ServiceConnection
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16");
@Autowired
Flyway flyway;
@Autowired
JdbcTemplate jdbc;
@Test
void allMigrationsAreApplied() {
assertEquals(0, flyway.info().pending().length);
}
@Test
void displayNameColumnExists() {
Integer count = jdbc.queryForObject(
"SELECT count(*) FROM information_schema.columns "
+ "WHERE table_name = 'app_user' AND column_name = 'display_name'",
Integer.class);
assertEquals(1, count);
}
}@ServiceConnection replaces the datasource URL, username, and password with the container's values, so you do not set them in application.yml. If a migration contains a SQL error, the context fails to start and every test in the class fails. That failure is the signal you want.
What to assert
The empty-pending check proves the migrations ran, but it says little about their results. Add assertions that match the risk of each change:
- A new column or table exists, as in
displayNameColumnExists. - A backfill, such as
V3__Backfill_display_name, produced the expected rows. Insert test data before the migration only if you run Flyway manually with a lowertargetversion. - Repeatable migrations such as
R__active_users_view.sqlcan be queried without error.
Common errors
| Symptom | Likely cause |
|---|---|
| `Could not find a valid Docker environment` | Docker is not running, or CI lacks Docker access |
| Context fails with a Flyway SQL error | A migration has a syntax error or depends on a missing object |
| `Schema-validation: missing column` | Hibernate `ddl-auto: validate` found an entity field with no migration |
| Tests pass locally but fail in CI | The container image tag differs from production, or migration files were not committed |
Pin the image tag to the major version your production database uses, so the test and production agree.
Exercise
Why is a migration test that uses H2 a weaker guarantee than one that uses a PostgreSQL Testcontainer, when production runs PostgreSQL?
Show answer
H2 accepts different SQL and has different types and functions than PostgreSQL. A migration can succeed on H2 and still fail or behave differently on PostgreSQL. The Testcontainer runs the same engine as production, so a passing test shows the migrations work on the database you actually deploy.