Sub topic 7
Test slices: loading only what you need
This chapter assumes you can write a basic @SpringBootTest, understand beans and auto-configuration, and know what a unit test with Mockito looks like. It explains what a test slice is and how to choose one.
The problem slices solve
@SpringBootTest starts the whole application context. That includes every controller, service, repository, and auto-configured component. The test is realistic, but it is slow, and a failure in one corner can break unrelated tests.
A test slice loads only the part of the context that one layer needs. Spring Boot provides a family of annotations for this. Each one does three things:
- It registers only the beans for that layer, such as controllers, JSON converters, or repositories.
- It applies only the auto-configuration for that layer.
- It leaves out everything else, such as
@Serviceand@Componentbeans.
The result is a smaller context that starts faster and tests one concern.
Common slice annotations
| Annotation | What it loads | Typical use |
|---|---|---|
| `@WebMvcTest` | Controllers, `@ControllerAdvice`, filters, `MockMvc` | Request mapping, validation, status codes |
| `@DataJpaTest` | Entities, Spring Data repositories, `EntityManager`, `DataSource` | Queries and mappings |
| `@JsonTest` | Jackson `ObjectMapper` and JSON helpers | Serialization of DTOs |
| `@RestClientTest` | `RestTemplate` or `RestClient` builders and a mock server | HTTP clients |
Later chapters cover each one in detail. Here we focus on the shared mechanics.
How a slice decides what to load
Each slice annotation is meta-annotated with @TypeExcludeFilters, which filters component scanning. For example, @WebMvcTest keeps @Controller beans and drops @Service beans. Each slice also uses @AutoConfigureXxx annotations to turn on only the matching auto-configuration.
This has a direct consequence. If a controller depends on a service, the service is not in the context, so the test fails at startup with a NoSuchBeanDefinitionException. You must supply that dependency yourself, usually as a mock.
@WebMvcTest(GreetingController.class)
class GreetingControllerTest {
@Autowired
MockMvc mockMvc;
@MockitoBean
GreetingService service;
}Use @MockitoBean in Spring Boot 3.4 and later. Older versions use @MockBean.
Seeing the difference
The diagram compares what each kind of test starts.
Common errors
- Missing bean at startup. A slice does not scan
@Servicebeans. Add@MockitoBeanfor each collaborator, or use@Importfor a real one. - Expecting a bean that the slice excludes. Autowiring a service into a
@DataJpaTestfails for the same reason. - Unexpected transactions.
@DataJpaTestrolls back each test by default. Data saved in one test is not visible in the next. - Replaced database.
@DataJpaTestswaps in an embedded database unless you set@AutoConfigureTestDatabase(replace = NONE). This matters when you need a real engine. - Choosing the wrong slice. If a test needs several layers together, a slice is the wrong tool. Use
@SpringBootTest.
Adding extra beans
When a slice needs one more real bean, import it explicitly.
@DataJpaTest
@Import(AuditConfig.class)
class AuditedRepositoryTest {
// AuditConfig is now part of this slice
}@Import adds just that class. It does not widen the scan to everything else.
Exercise
A @WebMvcTest for OrderController fails at startup with NoSuchBeanDefinitionException for OrderService. Why does this happen, and what are two ways to fix it?
Show answer
The web slice scans controllers but not @Service beans, so OrderService is not in the context. Fix it by declaring @MockitoBean OrderService service (@MockBean before Spring Boot 3.4) to supply a mock, or by adding @Import(OrderService.class) to load the real class, which in turn needs its own dependencies available.