I am creating tests with @WebMvcTest annotation and found that if I have a @ComponentScan annotation in the application class it will break the expected behavior of the tests.
According to the WebMvcTest javadoc:
Using this annotation will disable full auto-configuration and instead apply only configuration relevant to MVC tests (i.e.
@Controller,@ControllerAdvice,@JsonComponent Filter,WebMvcConfigurerandHandlerMethodArgumentResolverbeans but not@Component,@Serviceor@Repositorybeans)."
The problem is that with @ComponentScan it is instantiating beans annotated with @Service. If instead of @ComponentScan I specify the scan base packages in the @SpringBootApplication annotation everything works as expected.
Another problem happens when I specify the controller class in the @WebMvcTest annotation. When there is a @ComponentScan annotation in the application class it will load all controllers instead of loading only the specified one.
Is this a bug in Spring Boot?
I want to use @ComponentScan because of the excludeFilters attribute which is not available in the @SpringBootApplication annotation.
A workaround I have found is to create a separate class with @Configuration annotation and move the @ComponentScan there.
Found the reason for this odd behavior.
This is the declaration of the @SpringBootApplication annotation:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
As you can see the @ComponentScan annotation inside the @SpringBootApplication has the excludedFilters attribute specified.
When I added the @ComponentScan annotation directly in my application class I did not specify the default excludedFilters and that is why the behavior was different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With