Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ComponentScan in application class breaks @WebMvcTest and @SpringBootTest

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 and HandlerMethodArgumentResolver 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.

like image 379
Constantino Cronemberger Avatar asked Nov 01 '17 12:11

Constantino Cronemberger


1 Answers

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.

like image 89
Constantino Cronemberger Avatar answered Oct 14 '22 20:10

Constantino Cronemberger