Slice Testing & TestContainers
Utilize slice tests (`@WebMvcTest`, `@DataJpaTest`) for specific layers and integrate Testcontainers for external services.
Slice Testing: Focused Tests
Welcome to Lesson 3! In previous lessons, we learned about unit and full integration tests. Now, let's explore slice tests.
Slice tests focus on testing a specific 'slice' or layer of your application, like the web layer or the data layer, in isolation. This makes them:
- Faster: They load only necessary parts of the Spring context.
- More targeted: You test specific component interactions without the overhead of the entire application.
Testing Web Layers with @WebMvcTest
When you want to test your Spring MVC controllers and their interactions with HTTP requests, @WebMvcTest is your go-to annotation.
It auto-configures Spring MVC components (like @Controller, @RestController, @ControllerAdvice) but excludes other parts of your application, such as services and repositories. This ensures your test focuses solely on the web layer.
It uses MockMvc to simulate HTTP requests and responses without starting a full HTTP server.
All lessons in this course
- Unit Testing with JUnit & Mockito
- Integration Testing with Spring Boot
- Slice Testing & TestContainers
- Testing Web Controllers with MockMvc