相关疑难解决方法(0)

在 SpringBoot 集成测试中使用 TestContainers 填充数据库

我正在测试 TestContainers,我想知道如何填充执行 .sql 文件的数据库以创建结构并添加一些行。

怎么做?

@Rule
public PostgreSQLContainer postgres = new PostgreSQLContainer();
Run Code Online (Sandbox Code Playgroud)

spring-boot testcontainers

12
推荐指数
5
解决办法
1万
查看次数

在 Spring Boot 测试中正确使用 TestPropertyValues

我遇到了TestPropertyValues,这里的 Spring Boot 文档中简要提到过:https : //github.com/spring-projects/spring-boot/blob/2.1.x/spring-boot-project/spring-boot-docs/src /main/asciidoc/spring-boot-features.adoc#testpropertyvalues

此处的迁移指南中也提到了这一点:https : //github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#environmenttestutils

这两个示例都显示了一个environment将属性应用到的变量,但我找不到其他文档。

在我的测试中,属性设置来得太晚,无法影响@ValueSpring Bean的属性注入(via )。换句话说,我有一个这样的构造函数:

  public PhoneNumberAuthorizer(@Value("${KNOWN_PHONE_NUMBER}") String knownRawPhoneNumber) {
    this.knownRawPhoneNumber = knownRawPhoneNumber;
  }
Run Code Online (Sandbox Code Playgroud)

由于上面的构造函数在测试代码有机会运行之前被调用,因此TestPropertyValues在构造函数中使用它之前,无法在测试中更改属性 via 。

我知道我可以使用propertiesfor 参数@SpringBootTest,它在创建 bean 之前更新环境,那么 的适当用法是TestPropertyValues什么?

java spring-boot spring-boot-test

5
推荐指数
1
解决办法
3321
查看次数