ARTICLE AD BOX
I have a large Spring Boot application with an extensive test suite (unit tests + controller tests).
After introducing a new HandlerInterceptor, many of my previously passing tests suddenly started failing. Most of these tests were not written with interceptors in mind, and now the interceptor’s logic is being executed for almost every request inside the tests.
I'm looking for guidance on how to fix or isolate the interceptor for tests, and what the recommended approach is:
Should I disable the interceptor in certain tests?
Should I mock the service the interceptor depends on?
Should I update every test to account for the interceptor?
Is there a way to automatically exclude it in MockMvc/WebMvcTest setups?
Below is a simplified example of the configuration and interceptor. (Names changed.)
@Configuration @RequiredArgsConstructor public class DemoPathConfig implements WebMvcConfigurer { private final DemoAccessInterceptor demoAccessInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(demoAccessInterceptor) .addPathPatterns("/api/internal/**"); } } @Component @RequiredArgsConstructor @Slf4j public class DemoAccessInterceptor implements HandlerInterceptor { private final DemoCheckService demoCheckService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { if (HttpMethod.OPTIONS.matches(request.getMethod())) { return true; } demoCheckService.validateAccess(); return true; } }What is the best way to handle the sudden test failures after adding an interceptor?
I’m especially interested in strategies used in large codebases, such as disabling interceptors globally for tests, conditional registration, or MockMvc/WebMvcTest configuration tricks.
