带有@WebMvcTest 的测试切片正在加载大量与目标无关的控制器 [英] Test slice with @WebMvcTest is loading a substantial amount of controllers unrelated with the target

查看:21
本文介绍了带有@WebMvcTest 的测试切片正在加载大量与目标无关的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 spring boot 应用程序,它产生了许多控制器,我的目标是为特定的控制器创建一个集成测试.我读到我们可以使用 @WebMvcTest 注释实现测试切片,该注释仅加载部署目标控制器所需的内容,这个假设是否正确?这是我的测试:

I have a spring boot application yielding numerous controllers and my goal is to create an integration test for a specific one. I read that we can achieve a test slice with the @WebMvcTest annotation that loads only what is necessary to deploy the target controller, is this assumption correct? Here is my test:

@RunWith(SpringRunner.class)
@WebMvcTest(
        controllers = {DummyController.class},
)
public class DummyControllerIT {

    @Autowired
    private MockMvc mockMvc;

...

不幸的是,执行尝试部署与目标控制器无关的其他控制器/服务/存储库,这迫使我在每个控制器上使用 @MockBean.我的印象是 @WebMvcTest 会让我免于使用 @MockBean 注释声明的控制器/服务/存储库的广泛列表,我错了吗?

Unfortunately the execution attempts to deploy other controllers/services/repositories that have no relation to the target Controller, which forces me use @MockBean on each of them. I was under the impression that @WebMvcTest would spare me from having an extensive listing of declared controllers/services/repositories with the @MockBean annotation, am I wrong?

如果我误解了这一点并且我应该在应用程序的不相关部分使用 @MockBean,那么 为什么使用 @WebMvcTest 更好code> 而不是 @SpringBootTest?另一方面,如果我正确解释了它,我错过了什么?

If I misinterpreted this and I am expected to use @MockBean on unrelated parts of the application, then why is it better to use @WebMvcTest instead of @SpringBootTest? On the other hand, if I interpreted it correctly what am I missing?

不确定它是否相关,但这是我的初始化程序:

Not sure if it is related but this is my initialiser:

@ComponentScan(scopedProxy = ScopedProxyMode.INTERFACES)
@SpringBootApplication
@EnableTransactionManagement
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableScheduling
@EnableCaching
@EnableJpaAuditing
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
public class Application extends SpringBootServletInitializer {

    @Autowired
    private Environment env;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    @Bean
    @Primary
    public TaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(Integer.parseInt(Objects.requireNonNull(env.getProperty("coreThreadPoolSize"))));
        executor.setMaxPoolSize(Integer.parseInt(Objects.requireNonNull(env.getProperty("maxThreadPoolSize"))));
        executor.initialize();
        return executor;
    }

}

感谢您的帮助.

推荐答案

@WebMvcTest 的工作原理是禁用应用程序的完全自动配置并过滤其组件扫描,以便只扫描需要的部分被配置.您的主类 Application 启用自动配置(通过 @SpringBootApplication),但也明确启用组件扫描以及缓存、安全性、JPA 存储库等.@WebMvcTest 不会关闭这些显式启用的部分,因此您不得不进行模拟.

@WebMvcTest works by disabling full auto-configuration of your application and also filtering its component scanning so that only the parts that are needed are configured. Your main class, Application, is enabling auto-configuration (via @SpringBootApplication) but is also explicitly enabling component scanning as well as caching, security, JPA repositories, etc. @WebMvcTest doesn't switch off these explicitly enabled pieces so you're left having to mock things.

这个问题以及避免它的推荐方法是参考文档中的描述.简而言之,您应该将各种 @Enable... 注释移动到由组件扫描获取的单独 @Configuration 类.您可能还想查看其中一些的需求.例如,@EnableTransactionManagement 被自动配置覆盖.

This problem and the recommended way to avoid it is described in the reference documentation. In short, you should move the various @Enable… annotations to separate @Configuration classes that are picked up by component scanning. You may also want to review the need for some of them. For example, @EnableTransactionManagement is covered by auto-configuration.

您对 @ComponentScan 的使用也存在问题,因为它会关闭 @WebMvcTest 所需的过滤.将它从 @SpringBootApplication 注释的类移开应该可以解决这部分问题.

Your use of @ComponentScan is also problematic as it switches off the filtering that @WebMvcTest requires. Moving it away from the @SpringBootApplication-annotated class should fix this part of the problem.

这篇关于带有@WebMvcTest 的测试切片正在加载大量与目标无关的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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