liveBook · Manning

@ModuleSlicing

As explained in Testing Spring Boot Applications

@ModuleSlicing is a Spring Modulith annotation that limits Spring Boot test slices to a specified application module.

@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.

How Testing Spring Boot Applications covers it

How deep it goes
Explains annotation composition, automatic controller discovery, excluded layers, and replacement beans, but not custom slice implementation.
Examples
Uses City module controller tests with @WebMvcTest, MockMvcTester, and a mocked CityService.
What it assumes
Assumes Spring Boot test slices and Spring Modulith modules, while leaving dependency replacement explicit.
What you can do after
Builds fast module-scoped web tests by combining @ModuleSlicing with a Spring Boot test-slice annotation.
How it connects
Connects Spring Boot test slices with @WebMvcTest, @ApplicationModuleTest, @SpringBootTest, and module boundaries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Testing Spring Boot Applications. The section links point to the parts of the book it draws on.