Testing

Тестирование backend на Java/Spring простыми словами: пирамида тестов, интеграционные тесты с Testcontainers и работа с моками и внешними системами (WireMock).

A compact section on the testing strategy for a Java/Spring backend: which tests to write and where, how to test against a real database via Testcontainers, and how to deal with external systems. It complements the article on testing in Spring and the test-strategy style guide.

  • The test pyramid: what and where — Unit, integration, and e2e — three levels of testing. The pyramid shows which tests you should have many of, which few, and why this matters for the speed of the feedback loop.
  • Integration tests with Testcontainers — Why spin up a real PostgreSQL in tests instead of H2 and mocks, how @SpringBootTest and slices work, and how @ServiceConnection simplifies Testcontainers in Spring Boot 3.1+.
  • Mocks and external systems in tests — When to use Mockito and when WireMock — we look at stubs for external HTTP services, data isolation, and test determinism.