Testing for Spring Boots
Testing in Spring Boot uses JUnit 5, Mockito and the spring-boot-starter-test toolkit. Slice tests such as @WebMvcTest and @DataJpaTest load only part of the context for speed, while @SpringBootTest starts the full application. Testcontainers can run a real database in Docker, so integration tests behave like production.
Stage: Quality and Production · Part of the Spring Boot roadmap · Last reviewed 19 November 2026
What to learn in Testing
- Test a controller in isolation with @WebMvcTest and MockMvc
- Verify a repository query with @DataJpaTest against a real database
- Replace a bean with @MockBean or a Mockito mock in a test
- Run a PostgreSQL container with Testcontainers for integration tests
- Choose between slice tests and full @SpringBootTest for a given case
Testing interview questions
What is the difference between @WebMvcTest and @SpringBootTest?
@WebMvcTest loads only the web layer, controllers, filters and converters, and expects service beans to be mocked, so it runs quickly. @SpringBootTest starts the entire application context, optionally with a real port, and is slower but verifies that all parts work together.
Why use Testcontainers instead of an in-memory H2 database?
H2 differs from production databases in SQL dialect, types and locking, so tests can pass while production fails. Testcontainers starts the same database engine, such as PostgreSQL, in Docker for each test run, giving results much closer to real behaviour.
Where Testing fits in the Spring Boot roadmap
Testing belongs to the Quality and Production stage (advanced level), which takes about 21 hours. Operate it.
Other units in this stage
Back to the full Spring Boot roadmap