@ModuleSlicing restricts a Spring Boot test slice to one Spring Modulith application module. Combined with a slice such as @WebMvcTest, it loads the slice’s relevant infrastructure only within that module. section 5.5.2 section 5.8
Use the annotation when:
- Replacing dependencies remains necessary because other application layers are excluded; use mechanisms such as @MockitoBean or @TestBean. section 5.8
- Choosing between approaches, remember that @ApplicationModuleTest provides a full Spring Boot-style context for one module, while @ModuleSlicing narrows an existing Boot slice. section 5.5.2
Slice the City module’s controllers
@ModuleSlicing @WebMvcTest class CityModuleControllersTests { @MockitoBean CityService cityService; @Autowired MockMvcTester mvc; // ... tests ... } The test automatically covers the module’s controllers while keeping unrelated modules and non-web layers outside the slice.
