Integration testing
On this page 8 topics
Overview
Topics
- Spring Boot Test
- real database testing
- Testcontainers
Learn first
3 h of practice (estimate)
Learning outcomes
- Write
@SpringBootTestand@DataJpaTesttests against a real PostgreSQL using Testcontainers. - Use service connections so tests configure themselves from containers.
- Isolate test data between tests (transactions, cleanup, unique data).
- Explain why H2 is not a substitute for the production database.
Lessons
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)
| ID | Outcome | Task |
|---|---|---|
| JNR-045-EX01 | 1 | Write a @DataJpaTest with a Testcontainers PostgreSQL for findByCategoryOrderByPrice. |
| JNR-045-EX02 | 1 | Write a @SpringBootTest (random port) that places an order and checks it is stored. |
| JNR-045-EX03 | 2 | Replace a @DynamicPropertySource setup with @ServiceConnection. |
| JNR-045-EX04 | 3 | Two tests insert the same unique email. Fix with unique data and cleanup. |
| JNR-045-EX05 | 3 | Explain why @Transactional on a test does not clean up data created through real HTTP. |
| JNR-045-EX06 | 4 | List three differences between H2 and PostgreSQL that can hide bugs. |
| JNR-045-EX07 | 1 | Make one container serve all test classes and time the suite before and after. |
Show solutions
- EX01
@DataJpaTest @AutoConfigureTestDatabase(replace = NONE) @Testcontainerswith astatic @Container @ServiceConnectionfield; save products and assert order. - EX02
@SpringBootTest(webEnvironment = RANDOM_PORT), post throughTestRestTemplate, then assert with the repository. - EX03 Remove the
@DynamicPropertySourcemethod and annotate the container field with@ServiceConnection. - EX04 Generate
user-<uuid>@test.comper test; addrepo.deleteAll()in@BeforeEachwhere 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
staticcontainer 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.
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.