|
1 | 1 | package com.example.solidconnection.support; |
2 | 2 |
|
3 | | -import jakarta.annotation.PostConstruct; |
4 | | -import org.springframework.boot.test.context.TestConfiguration; |
5 | | -import org.springframework.test.context.DynamicPropertyRegistry; |
6 | | -import org.springframework.test.context.DynamicPropertySource; |
| 3 | +import org.springframework.boot.test.util.TestPropertyValues; |
| 4 | +import org.springframework.context.ApplicationContextInitializer; |
| 5 | +import org.springframework.context.ConfigurableApplicationContext; |
7 | 6 | import org.testcontainers.containers.GenericContainer; |
8 | | -import org.testcontainers.junit.jupiter.Container; |
9 | 7 |
|
10 | | -@TestConfiguration |
11 | | -public class RedisTestContainer { |
| 8 | +public class RedisTestContainer implements ApplicationContextInitializer<ConfigurableApplicationContext> { |
12 | 9 |
|
13 | | - @Container |
14 | | - private static final GenericContainer<?> CONTAINER = new GenericContainer<>("redis:7.0"); |
| 10 | + private static final int ORIGINAL_PORT = 6379; |
| 11 | + private static final GenericContainer<?> CONTAINER = new GenericContainer<>("redis:7.0") |
| 12 | + .withExposedPorts(ORIGINAL_PORT); |
15 | 13 |
|
16 | | - @DynamicPropertySource |
17 | | - static void redisProperties(DynamicPropertyRegistry registry) { |
18 | | - registry.add("spring.redis.host", CONTAINER::getHost); |
19 | | - registry.add("spring.redis.port", CONTAINER::getFirstMappedPort); |
| 14 | + static { |
| 15 | + CONTAINER.start(); |
20 | 16 | } |
21 | 17 |
|
22 | | - @PostConstruct |
23 | | - void startContainer() { |
24 | | - if (!CONTAINER.isRunning()) { |
25 | | - CONTAINER.start(); |
26 | | - } |
| 18 | + @Override |
| 19 | + public void initialize(ConfigurableApplicationContext applicationContext) { |
| 20 | + TestPropertyValues.of( |
| 21 | + "spring.data.redis.host=" + CONTAINER.getHost(), |
| 22 | + "spring.data.redis.port=" + CONTAINER.getMappedPort(ORIGINAL_PORT) |
| 23 | + ).applyTo(applicationContext.getEnvironment()); |
27 | 24 | } |
28 | 25 | } |
0 commit comments