Closed
Description
There's a bit of ceremony involved at the moment:
@Testcontainers
@ContextConfiguration(initializers = DataRedisTestIntegrationTests.Initializer.class)
@DataRedisTest
public class DataRedisTestIntegrationTests {
@Container
public static RedisContainer redis = new RedisContainer();
// …
static class Initializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(
ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of("spring.redis.port=" + redis.getMappedPort())
.applyTo(configurableApplicationContext.getEnvironment());
}
}
It would be nice to be able to do something like this instead:
@Testcontainers
@DataRedisTest
public class DataRedisTestIntegrationTests {
@Container
@MapPortToProperty("spring.redis.port")
public static RedisContainer redis = new RedisContainer();
A few thoughts:
- The examples above are JUnit 5. We'd need to consider JUnit 4 as well
- The lifecycle ordering will need to be right so that the container's port is available before the context refreshes
- Setting the port will affect context caching
/cc @bsideup