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
21 EnvironmentTestUtils.addEnvironment("testcontainers", applicationContext.getEnvironment(),
22 "spring.redis.host=" + containerIpAddress,
23 "spring.redis.port=" + mappedPort
24 );
25
26
27
28
29
30
31
32 return null;
33 };
34 }