View Javadoc
1   package top.infra.test.containers;
2   
3   import static lombok.AccessLevel.PRIVATE;
4   
5   import lombok.NoArgsConstructor;
6   
7   import org.springframework.boot.test.util.EnvironmentTestUtils;
8   import org.springframework.context.ConfigurableApplicationContext;
9   import org.testcontainers.containers.GenericContainer;
10  
11  import java.util.function.BiFunction;
12  
13  @NoArgsConstructor(access = PRIVATE)
14  public final class InitializerCallbacks {
15  
16      public static final BiFunction<GenericContainer, ConfigurableApplicationContext, Void> SPRING_DATA_REDIS = (container, applicationContext) -> {
17          final String containerIpAddress = container.getContainerIpAddress();
18          final int mappedPort = container.getMappedPort(6379);
19  
20          // spring-boot 1.5.x
21          EnvironmentTestUtils.addEnvironment("testcontainers", applicationContext.getEnvironment(), //
22              "spring.redis.host=" + containerIpAddress, //
23              "spring.redis.port=" + mappedPort
24          );
25          // spring-boot 2.0.x
26          //TestPropertyValues values = TestPropertyValues.of(
27          //    "spring.redis.host=" + redis.getContainerIpAddress(),
28          //    "spring.redis.port=" + redis.getMappedPort(6379)
29          //);
30          //values.applyTo(configurableApplicationContext);
31  
32          return null;
33      };
34  }