Spring集成测试。如何使用@Sql与动态模式名称相结合

问题描述 投票:0回答:1

我正在考虑在我的集成测试中使用@Sql来清理测试用例前的数据。注释解决了我的用例。

唯一需要注意的是,我无法弄清楚如何传递模式名称,因为每个环境下的模式名称对我来说都是不同的。

 @Test
  @Sql({"classpath:sql/clean_up_script.sql"})
  public void testEntireJob() throws Exception {

   //test case

  }
}

Clean_up_script.sql

truncate table schema_name.sample;

在SQL中,我不想硬编码模式名。有没有一种方法,我可以通过参数作为模式名称。

P.S 我正在使用flyway进行DB迁移,但这个脚本不是flyway的一部分。

java spring spring-boot spring-data-jpa spring-test
1个回答
0
投票

你可以提供一个 application.properties 文件夹 src/test/resources 并指定整个测试执行的模式与。(假设你使用 @SpringBootTest 来启动你的测试的整个Spring上下文)。)

spring.jpa.properties.hibernate.default_schema=my_schema

也有一个注解可用于 直列 这个配置。@TestPropertySource(properties = "spring.jpa.properties.hibernate.default_schema=my_schema")

你也可以指定模式的名称作为JDBC连接字符串的一部分。

jdbc:postgresql://localhost:5432/mydatabase?currentSchema=my:schema

用这种方法,你的 .sql 文件可以是无模式的,配置在一个中心位置。

© www.soinside.com 2019 - 2024. All rights reserved.