QuickDevGuide

Home › Spring Boot

Testing Spring Boot Applications — Spring Boot

Annotation Loads Use for none (plain JUnit + Mockito) nothing services and domain logic — fastest @WebMvcTest(Controller.class) MVC layer only controllers, validation, JSON, security rules @DataJpaTest JPA + repositories queries and mappings @JsonTest Jackson serialisation @SpringBootTest whole context end-to-end slices, wiring Testcontainers (@ServiceConnection) real Postgres/Kafka/Redis in Docker realistic integration tests Mock collaborators in slice tests with @MockitoBean (Boot 3.4+; @MockBean earlier). Use MockMvc (or RestTestClient/WebTestClient) to call endpoints. Keep the pyramid: many fast unit tests, fewer slice tests, a handful of full integration tests.

Key Takeaways