mab*_*abn 6 java junit spring automated-tests spring-boot
我想覆盖测试中application.properties中定义的属性,但@TestPropertySource只允许提供预定义的值.
我需要的是在随机端口N上启动服务器,然后将此端口传递给spring-boot应用程序.端口必须是短暂的,以允许同时在同一主机上运行多个测试.
我不是指嵌入式http服务器(jetty),而是在测试开始时启动的一些不同的服务器(例如zookeeper)和被测试的应用程序必须连接到它.
实现这一目标的最佳方法是什么?
(这里是一个类似的问题,但答案没有提到临时端口的解决方案 - 在Junit Test中覆盖默认的Spring-Boot application.properties设置)
从 Spring Framework 5.2.5 和 Spring Boot 2.2.6 开始,您可以Dynamic Properties在测试中使用:
@DynamicPropertySource
static void dynamicProperties(DynamicPropertyRegistry registry) {
registry.add("property.name", "value");
}
Run Code Online (Sandbox Code Playgroud)
由于 Spring Framework 5.2.5 中所做的更改,@ContextConfiguration 和 ApplicationContextInitializer 的使用可以替换为具有相同目的的静态 @DynamicPropertySource 方法。
@SpringBootTest
@Testcontainers
class SomeSprintTest {
@Container
static LocalStackContainer localStack =
new LocalStackContainer().withServices(LocalStackContainer.Service.S3);
@DynamicPropertySource
static void initialize(DynamicPropertyRegistry registry) {
AwsClientBuilder.EndpointConfiguration endpointConfiguration =
localStack.getEndpointConfiguration(LocalStackContainer.Service.S3);
registry.add("cloud.aws.s3.default-endpoint", endpointConfiguration::getServiceEndpoint);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以像这样覆盖端口属性的值@BeforeClass:
@BeforeClass
public static void beforeClass() {
System.setProperty("zookeeper.port", getRandomPort());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1580 次 |
| 最近记录: |