在整体式 SpringBoot 应用程序中创建集成测试 [英] Creating integration tests in a monolithic SpringBoot application

查看:23
本文介绍了在整体式 SpringBoot 应用程序中创建集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求为一个非常大的 SpringBoot 项目中的服务创建一个集成测试,该项目产生了数十个已实现的服务.当应用程序被执行时,所有这些服务都会被部署——我想避免部署与我正在为其创建测试的服务无关的所有服务.不幸的是,我(还)没有像我希望的那样在 Spring Boot 测试方面拥有丰富的经验,因此我想知道解决这个问题的最佳方法是什么.

I was asked to create an integration test for a service within a very large SpringBoot project yielding dozens of implemented services. When the application is executed all of these services are deployed - I want to avoid deploying all services unrelated with the one for which I'm creating the test. Unfortunately, I do not (yet) have as much experience with spring boot tests as I would hope for hence I was wondering what is the best approach to address this.

我正在考虑使用 @MockBean 注释来注释所有不相关的服务,并在测试类中使用 @Autowire 注释所有相关的服务,但我不确定这是否是正确的方法.有人能指出我正确的方向吗?

I was thinking about annotating all unrelated services with @MockBean annotation and all related ones with @Autowire inside the test class, but I'm not sure if this is the proper way to go. Can anyone point me in the right direction?

谢谢.

推荐答案

答案很大程度上取决于集成测试的范围.我将尝试涵盖两种主要方式,您可以在 google 上搜索更多示例和详细信息.Spring Boot 测试文档也是你的朋友.

The answer largely depends on the scope of your integration test. I will try to cover two main ways and you can google your wait our for more examples and details. Spring Boot testing documentation is also your friend.

切片

Spring Boot 提供了名为 切片.例如,有一个用于测试控制器的切片 - @WebMvcTest - 此测试将加载用于从 HTTP 调用您的应用程序的所有配置以及您的指定的控制器 (@WebMvcTest(YourController.class)).之后,您需要决定如何处理该控制器的依赖项.

Spring Boot provides test utilities called slices. For example there's a slice for testing your controllers - @WebMvcTest - this test will load all configuration for calling your application from HTTP and your specified controller (@WebMvcTest(YourController.class)). After that you need to decide what to do with dependencies of that controller.

您可以:

  • @MockBean 模拟它们.
  • 使用 @Import 提供真正的实现(或额外的配置)(然后你必须再次处理新导入的依赖项的依赖项).
  • 加载 Spring Boot 自动配置的附加部分.这可以使用 @AutoConfigureSomething 注释来完成.- 所有切片基本上都是自动配置注释的组合,您可以自由地将它们添加到您的测试中.例如看看 DataJpaTest 上的注释,了解添加使用测试数据库设置 Spring Boot Data JPA 所需的功能.
  • Mock them with @MockBean.
  • Provide real implementation (or additional configuration) with @Import (and then you have to again deal with dependencies of the newly imported dependency).
  • Load additional part of Spring Boot auto-configuration. This can be done using @AutoConfigureSomething annotations. - All slices are basically composites of autoconfigure annotations and you are free to add them to your tests. For example have a look at annotations on DataJpaTest for what it takes to add capability to setup Spring Boot Data JPA with test database.

每个测试最多可以有一个切片,但您可以导入任意数量的附加服务、配置、模拟、自动配置等.重点是 - 您选择为测试配置的内容;具有新依赖项的新无关服务不应破坏现有测试.

You can have maximum one slice per your test but you can import any number of additional services, configurations, mocks, auto-configurations etc. The point is - you choose what is configured for your test; new unrelated services with new dependencies should not break existing tests.

SpringBootTest

另一种方法是 @SpringBootTest 注释 - 这是相反的方向 - 默认情况下它会加载所有内容,您可以使用 @MockBean 排除您不想要的内容,@EnableAutoConfiguration(exclude=SomeClass)

Another approach is @SpringBootTest annotation - this goes in the opposite direction - by default it loads everything and you can exclude stuff you don't want with @MockBean, @EnableAutoConfiguration(exclude=SomeClass) etc.

在添加新服务时,当然存在破坏现有测试的危险.- 这不应该经常发生,因为一切都是自动配置的,但它仍然是一种可能性,尤其是在具有更多配置的单体中.

There's of course a danger of breaking existing tests when adding new services. - This should not happen too often as everything is configured automatically but it's still a possibility especially in monolith with more configuration.

这篇关于在整体式 SpringBoot 应用程序中创建集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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