Skip to content
Ameen Abozaid
JNR-045

Integration testing

  • 7 Lessons
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Spring Boot Test
  • real database testing
  • Testcontainers

Learn first

3 h of practice (estimate)

Learning outcomes

  1. Write @SpringBootTest and @DataJpaTest tests against a real PostgreSQL using Testcontainers.
  2. Use service connections so tests configure themselves from containers.
  3. Isolate test data between tests (transactions, cleanup, unique data).
  4. Explain why H2 is not a substitute for the production database.

Lessons

  1. 01What integration tests prove
  2. 02Testcontainers
  3. 03Repository tests with @DataJpaTest
  4. 04Full-application tests
  5. 05Test data isolation
  6. 06Demo: Testcontainers integration tests for Shop
  7. 07Demo: fixing leaking test data

Lab and exercises

Lab

JNR-045-LAB1 · Real-database tests

Outcomes: 1–4 · Time: ~3 h Write repository tests for every custom query and an end-to-end order test with migrations applied, all against PostgreSQL via Testcontainers. Acceptance: suite passes from a clean machine with only Docker installed.

Exercises (assessment: add integration tests for an untested repository)

IDOutcomeTask
JNR-045-EX011Write a @DataJpaTest with a Testcontainers PostgreSQL for findByCategoryOrderByPrice.
JNR-045-EX021Write a @SpringBootTest (random port) that places an order and checks it is stored.
JNR-045-EX032Replace a @DynamicPropertySource setup with @ServiceConnection.
JNR-045-EX043Two tests insert the same unique email. Fix with unique data and cleanup.
JNR-045-EX053Explain why @Transactional on a test does not clean up data created through real HTTP.
JNR-045-EX064List three differences between H2 and PostgreSQL that can hide bugs.
JNR-045-EX071Make one container serve all test classes and time the suite before and after.
Show solutions
  • EX01 @DataJpaTest @AutoConfigureTestDatabase(replace = NONE) @Testcontainers with a static @Container @ServiceConnection field; save products and assert order.
  • EX02 @SpringBootTest(webEnvironment = RANDOM_PORT), post through TestRestTemplate, then assert with the repository.
  • EX03 Remove the @DynamicPropertySource method and annotate the container field with @ServiceConnection.
  • EX04 Generate user-<uuid>@test.com per test; add repo.deleteAll() in @BeforeEach where needed.
  • EX05 The server handles the request on another thread with its own transaction, which commits; the test transaction does not cover it.
  • EX06 Examples: SQL dialect and functions, JSON/array/enum types, constraint and locking behavior, case sensitivity, migration scripts that only run on PostgreSQL.
  • EX07 A shared static container in an abstract base class or the singleton pattern; record before and after runtimes.
  • Outcome 4 is also covered by quiz Q09–Q10.

Quiz

Outcomes: O1 Testcontainers tests · O2 service connections · O3 data isolation · O4 H2 versus production database.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Integration tests mainly prove:O1
  2. 2Testcontainers needs which tool running?O1
  3. 3@DataJpaTest loads:O1
  4. 4To keep your PostgreSQL container with @DataJpaTest use:O1
  5. 5@ServiceConnection does what?O2
  6. 6Shared mutable data between tests causes:O3
  7. 7A good way to avoid data collisions:O3
  8. 8@Transactional on a test does not roll back data created:O3
  9. 9Why is H2 not a substitute for PostgreSQL?O4
  10. 10A query passing on H2 but failing in production shows:O4

Practical part

Seven exercises JNR-045-EX01 to EX07 plus the lab; for the assessment, add integration tests for an untested repository. Pass: quiz 8/10, at least five exercises correct, and the suite green on a clean machine with only Docker.

Common mistakes

Shared mutable data across tests; one container per test class without reuse.

Assessment

Add integration tests for an untested repository.

Career relevance

The foundation of a trustworthy CI pipeline.

Type to search levels, phases, modules and lessons.