Skip to main content

Testing

A Java backend engineer who cannot write good tests is considered junior regardless of other skills. This domain covers the full testing pyramid: fast unit tests with JUnit 5 and Mockito, integration tests with Spring Boot test slices, and realistic end-to-end tests using Testcontainers to spin up real databases in CI.

What You'll Find Here

TopicDescription
JUnit 5@Test, @ParameterizedTest, @BeforeEach, lifecycle, assertions, assumptions.
Mockito@Mock, @InjectMocks, when(...).thenReturn(...), verify, ArgumentCaptor.
Spring Boot Test Slices@WebMvcTest, @DataJpaTest, @JsonTest — fast partial-context tests.
Integration Tests@SpringBootTest with TestRestTemplate; full-context tests.
TestcontainersRunning real PostgreSQL, MySQL, Redis, Kafka in Docker containers during tests.
MockMvc & WebTestClientHTTP-level controller testing without a running server.

Learning Path

  1. JUnit 5 — the basics (@Test, @BeforeEach, assertThrows) are required before everything else.
  2. Mockito@Mock stub setup and verify interaction testing are the day-to-day tools.
  3. Spring Boot Test Slices@WebMvcTest and @DataJpaTest for fast, focused Spring tests.
  4. MockMvc & WebTestClient — test Spring MVC controllers without a real server; use with @WebMvcTest for speed.
  5. Testcontainers — the current gold standard for integration tests; Docker-based environments that match production.
  6. Integration Tests@SpringBootTest full-stack tests that tie all the above together.

Quick Revision

  • Spring Boot — Spring Boot test slices are the primary testing tool.
  • Databases — Testcontainers integrates with all major databases for realistic integration tests.
  • Docker — Testcontainers uses Docker under the hood.