有没有办法使用 Spring Boot、Liquibase 和 sql-script 进行集成测试?

Luk*_* M. 6 java spring hibernate liquibase spring-boot

我使用 liquibase 来设置我的数据库架构。我禁用休眠来创建任何东西。因此,我的 import.sql 被忽略。在 liquibase 创建表后,有没有办法配置 spring boot 或 liquibase 或任何其他部分来加载测试数据?

iku*_*men 0

如果您需要一些原始的东西(即,不是用于实际的数据迁移),您可以在 Spring Boot 中使用 Spring 的 JDBC 初始化程序。简而言之,您需要:

  1. 创建一个data-test.sql来加载您的数据,并将其放在您的src/main/resources目录中。对于不同的环境,只需使用命名约定即可data-{platform}.sql
  2. 添加applications-test.properties以下src/main/resources内容: spring.datasource.platform=test # this actives data-test.sql above spring.datasource.continueOnError=???? # depends on your needs
  3. 要在测试期间激活application-test.properties,请确保“test”是集成测试期间处于活动状态的配置文件之一。一种方法是用 注释您的测试类@ActiveProfiles({"test", ...})

  • 这并不能回答问题。仅当您不使用 Flyway 或 Liquibase 时,此解决方案才有效。问题是如何让Liquibase在Spring Boot中运行后运行sql脚本。如果您将 Liquibase 或 Flyway 与 Spring Boot 结合使用,Spring Boot 会关闭 Spring JDBC 初始化程序 (2认同)