Dynamic Property Registration
package org.example;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import java.util.function.Supplier;
@Slf4j
@SpringBootTest(classes = Application.class)
class SampleIT {
@Value("${service.client.url}")
private String testServiceUrlProperty;
@DynamicPropertySource
static void properties(DynamicPropertyRegistry registry) {
// Retrieve the property value dynamically (e.g., from a database or testcontainer)
String serverHost = "testcontainerHost";
int serverPort = 1080;
String path = "auth";
Supplier<Object> serviceUrl = () -> String.format("http://%s:%d/%s", serverHost, serverPort, path);
registry.add("service.client.url", serviceUrl);
}
@Test
void testDynamicProperties() {
log.info("testServiceUrlProperty: {}", testServiceUrlProperty);
Assertions.assertEquals("http://testcontainerHost:1080/auth", testServiceUrlProperty);
}
}

Last updated