应用程序类中的@ComponentScan 中断@WebMvcTest 和@SpringBootTest [英] @ComponentScan in application class breaks @WebMvcTest and @SpringBootTest

查看:33
本文介绍了应用程序类中的@ComponentScan 中断@WebMvcTest 和@SpringBootTest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 @WebMvcTest 注释创建测试,发现如果我在应用程序类中有 @ComponentScan 注释,它将破坏测试的预期行为.

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.

根据WebMvcTest javadoc:

According to the WebMvcTest javadoc:

使用此注解将禁用完全自动配置,而是仅应用与 MVC 测试相关的配置(即 @Controller@ControllerAdvice@JsonComponent FilterWebMvcConfigurerHandlerMethodArgumentResolver bean 但不包括 @Component@Service@Repository bean)."

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)."

问题在于,@ComponentScan 是实例化用 @Service 注释的 bean.如果我在 @SpringBootApplication 注释中指定扫描基础包而不是 @ComponentScan,一切都按预期工作.

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.

当我在 @WebMvcTest 注释中指定控制器类时,会发生另一个问题.当应用程序类中有 @ComponentScan 注释时,它将加载所有控制器,而不是仅加载指定的控制器.

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.

这是 Spring Boot 中的错误吗?

Is this a bug in Spring Boot?

我想使用 @ComponentScan 因为 excludeFilters 属性在 @SpringBootApplication 注释中不可用.

I want to use @ComponentScan because of the excludeFilters attribute which is not available in the @SpringBootApplication annotation.

我发现的一种解决方法是使用 @Configuration 注释创建一个单独的类并将 @ComponentScan 移到那里.

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.

这是@SpringBootApplication注解的声明:

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 {

如您所见,@SpringBootApplication 中的@ComponentScan 注解指定了excludedFilters 属性.

As you can see the @ComponentScan annotation inside the @SpringBootApplication has the excludedFilters attribute specified.

当我直接在应用程序类中添加 @ComponentScan 注释时,我没有指定默认的 excludeFilters,这就是行为不同的原因.

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.

这篇关于应用程序类中的@ComponentScan 中断@WebMvcTest 和@SpringBootTest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆