Skip to the page

Spring in production · mid

Integration tests and upgrading to Boot 4

Test with a real database in a container, keep the context cache working, and plan the Boot 3.5 to 4 upgrade in clear steps.

The notes

What you must know

  • Integration test: @SpringBootTest(webEnvironment = RANDOM_PORT) or MockMvc, @Testcontainers with a @Container @ServiceConnection PostgreSQL container. Call the API with RestTestClient (Boot 4), MockMvc or MockMvcTester; check 201, the Location header and the body; check the database through the repository or JdbcClient.
  • Flyway runs the real migrations against the container, so the schema is tested. Boot 4 needs spring-boot-starter-flyway.
  • With RANDOM_PORT the server runs on another thread, so a test's @Transactional rollback does not cover it. Clean data with unique values per test or truncate tables.
  • Slow and flaky suites: Spring caches application contexts for tests with the same configuration. Different @MockitoBean sets or properties create new contexts. Share base classes, avoid @DirtiesContext, use slice tests (@WebMvcTest, @DataJpaTest).
  • H2 and PostgreSQL differ in SQL, types and locking, so bugs hide. Use Testcontainers with a real database. Quarantine flaky tests with an owner and a deadline; never rerun until green.
  • Boot 4.0 and Framework 7.0 shipped in Nov 2025. Boot 4.2 with Framework 7.1 is current (19 Nov 2026). Free support for 3.5 ended on 30 Jun 2026. Boot 4 needs Java 17 or newer, and uses Jakarta EE 11, Hibernate 7 and Jackson 3.
  • Upgrade order: latest 3.5.x first and clear deprecation warnings; then Boot 4 with spring-boot-properties-migrator; one service first, canary, rollback ready.
  • Test changes: @MockBean and @SpyBean are removed; use @MockitoBean and @MockitoSpyBean. @SpringBootTest no longer sets up MockMvc by itself: add @AutoConfigureMockMvc. Other changes: Jackson 3 uses tools.jackson packages and a JsonMapper bean. Spring Security 7 accepts only the lambda DSL. RestTemplate is deprecated in 7.1; use RestClient. Framework 7 adds built-in API versioning.

The red pen

Common traps: mistake → fix

  • Mistake: H2 is close enough to production

    Fix: SQL and locking differ.

  • Mistake: Rerunning the failed test is a fix

    Fix: it hides shared state problems.

  • Mistake: Boot 4 is a small upgrade

    Fix: modules, Jackson 3, Hibernate 7 and test annotations all change.

Easy to hard

Questions interviewers ask, easy to hard

  1. Write an integration test for POST /orders against real PostgreSQL.
  2. The test suite takes 25 minutes and fails randomly. What do you check?
  3. What changed in Spring Boot 4 and Framework 7 that matters to you?
  4. How do you plan the move from Boot 3.5 to 4?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview