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
,WebMvcConfigurer
andHandlerMethodArgumentResolver
beans but not@Component
,@Service
or@Repository
beans)."
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.