0PricingLogin
Spring Boot 4 Complete Guide · Lesson

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

  1. Unit Testing with JUnit & Mockito
  2. Integration Testing with Spring Boot
  3. Slice Testing & TestContainers
  4. Testing Web Controllers with MockMvc
← Back to Spring Boot 4 Complete Guide