WebMvcTest attempts to load every application Controller

While taking a close look at your ComplaintController, you annotate it with @ControllerAdvice

The Javadoc of @WebMvcTest says the following about the relevant MVC beans that are part of the Spring Context for your test:

/**
 * Annotation that can be used for a Spring MVC test that focuses <strong>only</strong> on
 * Spring MVC components.
 * <p>
 * Using this annotation will disable full auto-configuration and instead apply only
 * configuration relevant to MVC tests (i.e. {@code @Controller},
 * {@code @ControllerAdvice}, {@code @JsonComponent},
 * {@code Converter}/{@code GenericConverter}, {@code Filter}, {@code WebMvcConfigurer}
 * and {@code HandlerMethodArgumentResolver} beans but not {@code @Component},
 * {@code @Service} or {@code @Repository} beans).
 * ...
 */

So any @ControllerAdvice is part of this MVC context and hence that’s the expected behaviour.

To fix this you could extract your exception handling out of your ComplaintController into a dedicated class that does depend on any other Spring Beans and can be instantiated without mocking anything.

PS: The word all is misleading inside the title of your question WebMvcTest attempts to load every application Controller.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top