Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A Spring Boot 3 test suite uses H2 and a Flyway migration with PostgreSQL-specific syntax: ALTER TABLE orders ADD COLUMN data jsonb. Tests fail. A teammate suggests a separate H2 migration folder. What do you recommend?

I would not add a separate H2 migration folder, because then the schema we test is not the schema we run in production, and real migration errors stay hidden until deployment. Instead, the integration tests should run against PostgreSQL with Testcontainers, using exactly the same Flyway migrations as production. Unit tests stay free of the database so they remain fast, the container is shared across test classes, H2 is kept only for genuinely portable code if at all, and the H2 console must never be enabled in production.

Why this question is asked

This is a common moment in a project's life. The team has a fast H2-based test suite, someone adds the first PostgreSQL-specific migration, and the build turns red. The quickest workaround is a second set of migrations for H2, and it looks harmless. Interviewers ask this because the right answer is a judgement about what tests are for, plus a practical plan that keeps the suite fast. In this answer, we will reproduce the failure, look at why the separate folder is a trap, set up Testcontainers with the same migrations, keep the suite fast, and cover the H2 console risk.

1Reproducing the Failure

Flyway runs migration scripts in version order at startup. The new script uses jsonb, PostgreSQL's binary JSON type, which H2 does not support the way PostgreSQL defines it, even in PostgreSQL compatibility mode.

SQL
-- src/main/resources/db/migration/V7__order_data.sql
ALTER TABLE orders ADD COLUMN data jsonb;
// Test startup on H2:
// FlywayMigrateException: Migration V7__order_data.sql failed
// ... Unknown data type: "JSONB" (H2's own error wording; check your version)
// The Spring context fails, so every test in the suite fails

Notice that the migration is correct. It works on PostgreSQL and is exactly what production needs. The test suite is failing because it tests against the wrong database, which is the real problem to solve.

2Why a Separate H2 Migration Folder Is a Trap

The suggestion is to point Flyway at db/migration for production and db/migration-h2 for tests, with V7 rewritten as, say, a VARCHAR column for H2. It makes the build green today. But from now on there are two schemas. Every future migration must be written twice, and nobody tests the production one: a typo in the PostgreSQL version of V8, a wrong index definition, a constraint that fails on existing data, all of these pass the tests and break on deployment.

Worse, the code is tested against a different column type. Queries that use jsonb operators such as ->> or @> cannot be tested at all, so the team either stops using them or stops testing them. The suite becomes a test of the H2 copy of our system, not of our system.

Separate H2 migrations versus the same migrations on PostgreSQL
QuestionSeparate H2 folderSame migrations on Testcontainers
Are production migrations tested?No, only the H2 copiesYes, every script, every run
Can queries use jsonb operators?Not testableYes
Work per new migrationWritten twice, kept in sync by handWritten once
Startup costUnder a secondA few seconds once per run

3Running Integration Tests on PostgreSQL With Testcontainers

Testcontainers starts PostgreSQL in Docker for the test run. We use the same major version as production and let Flyway run the real db/migration folder. Spring Boot 3.1 and later wires the container's connection details into the DataSource with @ServiceConnection, so no URL properties are needed.

Java
@TestConfiguration(proxyBeanMethods = false)
class TestDatabase {
@Bean @ServiceConnection
PostgreSQLContainer<?> postgres() {
return new PostgreSQLContainer<>("postgres:17");
}
}
@SpringBootTest
@Import(TestDatabase.class)
class OrderDataTest {
@Autowired JdbcClient jdbc;
@Test
void storesAndQueriesJsonb() {
jdbc.sql("UPDATE orders SET data = ?::jsonb WHERE id = 1").param("{\"gift\": true}").update();
Boolean gift = jdbc.sql("SELECT (data ->> 'gift')::boolean FROM orders WHERE id = 1")
.query(Boolean.class).single();
assertThat(gift).isTrue(); // true: V7 and the jsonb query are tested for real
}
}

We remove the H2 dependency from the test classpath at the same time, so no test can silently fall back to it. For @DataJpaTest slices, Boot normally replaces the DataSource with an embedded one; with a @ServiceConnection container imported, Boot 3.4 and later keep the container connection, and older versions need @AutoConfigureTestDatabase(replace = NONE), so check your version.

4Keeping the Suite Fast

The usual objection is speed, and it is fair. The answer is to structure the suite so the container cost is small and paid once. First, unit tests, for domain logic, validation and mapping, should not touch a database at all; they stay in milliseconds. Second, the container-backed integration tests share one container per run. Declaring it as a bean in one shared test configuration lets Spring's context cache reuse both the context and the container across test classes; a static container started once in a common base class does the same. A PostgreSQL container usually starts in a few seconds, once.

Third, we avoid making each test class a different Spring context, because each new context restarts the application and may start a new container. Keeping a small number of standard test configurations keeps the cache effective. With this structure, a suite of several hundred tests typically adds only seconds for the database, not minutes.

Step throughA test suite that stays fast and truthful
  1. Unit testsno database, milliseconds, most of the tests
  2. Repository and service integration testsone shared PostgreSQL container, real Flyway migrations
  3. A few end-to-end testsfull application against containers
  4. Productionthe same migrations, already tested

4 layers, from the top down. Step through them one at a time.

5If H2 Must Stay for Some Tests

Sometimes there is a reason to keep H2 for part of the suite, for example a module whose SQL is deliberately portable, or a CI environment where Docker is not yet available. Then we limit H2 to that code, document in the test class why it uses H2 and what it cannot prove, and make sure every migration and every PostgreSQL-specific query is still covered by container tests somewhere in the pipeline. The default for new tests should be PostgreSQL.

6A Note on the H2 Console

Teams that use H2 often enable its web console with spring.h2.console.enabled=true to look at data while developing. That console lets anyone who reaches it run arbitrary SQL against the database it is connected to. It must never be enabled in production, and it should not be switched on through a shared profile that might be active outside development. Spring Boot only enables it by default with devtools in development, and a security review should check that no production configuration sets it.

7Follow-up questions

Is it acceptable to keep H2 in CI and PostgreSQL locally?

It is the wrong way round. CI is the gate that decides whether code may be deployed, so it must test against the production engine. Local runs can be lighter, but CI should run the migrations and integration tests on PostgreSQL every time. Most CI systems support Docker, and Testcontainers also supports remote Docker hosts when the runner cannot start containers itself.

How do we test the migration against realistic data?

Testcontainers proves the migration runs on an empty schema. For a migration that changes existing data or adds a constraint, we also run it in a staging environment with a recent anonymised copy of production data, or restore such a dump into a container in a dedicated test. That catches problems like a NOT NULL constraint failing on old rows or a migration that locks a large table for too long.

Can Testcontainers reuse a container between test runs?

Testcontainers has a reuse feature that keeps a container running between runs when enabled in the developer's Testcontainers configuration and on the container. It speeds up local runs, but the data then persists between runs, so tests must not depend on a clean database. In CI we normally start fresh containers each run.

8Conclusion

In this answer, we saw that a separate H2 migration folder would make the build green by testing a copy of the schema instead of the real one, hiding migration errors and making PostgreSQL features untestable. The better fix is to run integration tests against PostgreSQL with Testcontainers and @ServiceConnection, using the same Flyway migrations as production. Unit tests without a database and one shared container keep the suite fast, H2 survives only for documented portable code if needed, and the H2 console must never be enabled in production.

What to say in the interviewAbout 90 seconds aloud

Open with

I would push back on the separate migration folder. It turns the test schema into a copy of the production schema, so the real migrations are never tested, every migration has to be written twice, and a mistake in the PostgreSQL version only shows up when we deploy.

Then, if they want more

It also means we can never test queries that use jsonb operators, which is the reason we added the column. The failing test is telling us we are testing against the wrong database. My recommendation is to run the integration tests on PostgreSQL with Testcontainers, same major version as production, and let Flyway run the same db/migration folder.

With Boot 3 I declare the container as a bean with @ServiceConnection in a shared test configuration, so Boot wires the DataSource and the context cache reuses one container for the whole run. I would remove H2 from the test classpath so nothing falls back to it.

To keep things fast, unit tests do not touch the database at all, and the integration tests share that one container, which starts in a few seconds. If some module really needs H2, I would limit it to portable SQL and document what those tests cannot prove.

And I would make sure spring.h2.console.enabled is never on in production, because the console lets anyone run SQL.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3.1
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
java.sql, package documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A Spring Boot 3 test suite uses H2 and a Flyway migration with PostgreSQL-specific...": https://scrivio-interview-library.netlify.app/databases/jdbc/5afe8a7f56/