Controller advice centralizes behavior shared across controllers, especially exception handling. In a Spring MVC test, it lets a focused web-layer test exercise error responses without loading the application’s full business logic. section 5.3
Handling a duplicate-city error
A controller advice catches CityAlreadySelectedException and returns HTTP 409 with the body "City already selected". The corresponding @WebMvcTest selects CityController, mocks CityService, triggers the exception, and verifies the conflict response through MockMvcTester. section 5.3
The focused test context includes controller advice among its recognized web components. @WebMvcTest can restrict the context to selected controllers while retaining the minimal infrastructure and testing utilities needed to exercise their web behavior. section 5.7.2 section 5.3
Practical testing choices include:
- Use @SpringBootTest when the application is small or full-context coverage remains readable and performant. section 5.3
- Use @WebMvcTest for isolated web edge cases when a larger application makes full-context tests unwieldy. section 5.3
- Customize included or excluded types only when the default slice selection does not meet the testing need. section 5.7.2

