【问题标题】:@SpringBootTest how to pre-populate embedded MongoDB?@SpringBootTest 如何预填充嵌入式 MongoDB?
【发布时间】:2020-03-10 23:07:56
【问题描述】:

基于Spring BootMongoDB 实现具有几个端点的微服务,并尝试使用@SpringBootTest 注释功能编写集成测试。

目前,我面临一个问题,我需要使用一些测试数据预先填充一个仅在“测试”短语中实例化的嵌入式 MongoDB 实例。

我在Spring Boot 中没有找到任何现成可用的选项。

有些人建议使用 mongobeemongoprefillnosql-unit 等测试数据预填充工具,但对我来说,这似乎是开销或解决方法,即使在测试范围内也不想引入任何新的依赖项.

所以您能否建议:在当前Spring Boot 生态系统中,当我们谈论与@987654331 进行集成(端到端)测试时,为了测试目的预填充MongoDB 的正确方法是什么@?

【问题讨论】:

    标签: mongodb spring-boot integration-testing spring-boot-test


    【解决方案1】:

    有多种方法可以预填充数据:

    1. 使用@BeforeEach@BeforeAll等JUnit生命周期方法填写数据
    2. 您可以禁用嵌入式 MongoDB 的 Spring Boot 自动配置并自行执行,并在创建连接后插入数据
    3. 您可以以某种方式反映我们用于测试关系数据库的@Sql 功能,并使用AsbtractTestExectuionListener 编写类似的东西。为此,请查看 Spring 类 SqlScriptsTestExecutionListener
    4. 提供一个实现CommandLineRunner 接口的类,并且仅使用@Profile("integration-test") 为您的集成测试配置文件激活此bean

    【讨论】:

      猜你喜欢
      • 2017-05-04
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 2014-04-10
      • 2018-01-26
      相关资源
      最近更新 更多