我正在测试 TestContainers,我想知道如何填充执行 .sql 文件的数据库以创建结构并添加一些行。
怎么做?
@Rule
public PostgreSQLContainer postgres = new PostgreSQLContainer();
Run Code Online (Sandbox Code Playgroud) 我遇到了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什么?