Prefer DynamicPropertyRegistar to DynamicPropertyRegistry
Closes gh-41996
This commit is contained in:
@@ -364,9 +364,9 @@ TIP: You can use the Maven goal `spring-boot:test-run` or the Gradle task `bootT
|
||||
[[features.dev-services.testcontainers.at-development-time.dynamic-properties]]
|
||||
==== Contributing Dynamic Properties at Development Time
|
||||
|
||||
If you want to contribute dynamic properties at development time from your `Container` `@Bean` methods, you can do so by injecting a `DynamicPropertyRegistry`.
|
||||
This works in a similar way to the xref:testing/testcontainers.adoc#testing.testcontainers.dynamic-properties[`@DynamicPropertySource` annotation] that you can use in your tests.
|
||||
It allows you to add properties that will become available once your container has started.
|
||||
If you want to contribute dynamic properties at development time from your `Container` `@Bean` methods, define an additional `DynamicPropertyRegistrar` bean.
|
||||
The registrar should be defined using a `@Bean` method that injects the container from which the properties will be sourced as a parameter.
|
||||
This arrangement ensures that container has been started before the properties are used.
|
||||
|
||||
A typical configuration would look like this:
|
||||
|
||||
|
||||
@@ -20,17 +20,22 @@ import org.testcontainers.containers.MongoDBContainer;
|
||||
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertyRegistrar;
|
||||
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
public class MyContainersConfiguration {
|
||||
|
||||
@Bean
|
||||
public MongoDBContainer mongoDbContainer(DynamicPropertyRegistry properties) {
|
||||
MongoDBContainer container = new MongoDBContainer("mongo:5.0");
|
||||
properties.add("spring.data.mongodb.host", container::getHost);
|
||||
properties.add("spring.data.mongodb.port", container::getFirstMappedPort);
|
||||
return container;
|
||||
public MongoDBContainer mongoDbContainer() {
|
||||
return new MongoDBContainer("mongo:5.0");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DynamicPropertyRegistrar mongoDbProperties(MongoDBContainer container) {
|
||||
return (properties) -> {
|
||||
properties.add("spring.data.mongodb.host", container::getHost);
|
||||
properties.add("spring.data.mongodb.port", container::getFirstMappedPort);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,18 +18,23 @@ package org.springframework.boot.docs.features.testcontainers.atdevelopmenttime.
|
||||
|
||||
import org.springframework.boot.test.context.TestConfiguration
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.test.context.DynamicPropertyRegistry
|
||||
import org.springframework.test.context.DynamicPropertyRegistrar;
|
||||
import org.testcontainers.containers.MongoDBContainer
|
||||
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
class MyContainersConfiguration {
|
||||
|
||||
@Bean
|
||||
fun mongoDbContainer(properties: DynamicPropertyRegistry): MongoDBContainer {
|
||||
var container = MongoDBContainer("mongo:5.0")
|
||||
properties.add("spring.data.mongodb.host", container::getHost)
|
||||
properties.add("spring.data.mongodb.port", container::getFirstMappedPort)
|
||||
return container
|
||||
fun mongoDbContainer(): MongoDBContainer {
|
||||
return MongoDBContainer("mongo:5.0")
|
||||
}
|
||||
|
||||
@Bean
|
||||
fun mongoDbProperties(container: MongoDBContainer): DynamicPropertyRegistrar {
|
||||
return DynamicPropertyRegistrar { properties ->
|
||||
properties.add("spring.data.mongodb.host") { container.host }
|
||||
properties.add("spring.data.mongodb.port") { container.firstMappedPort }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user