|
12 | 12 | import org.junit.jupiter.params.provider.NullAndEmptySource; |
13 | 13 | import org.junit.jupiter.params.provider.ValueSource; |
14 | 14 | import org.springframework.beans.factory.annotation.Autowired; |
| 15 | +import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails; |
| 16 | +import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; |
15 | 17 | import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; |
16 | 18 | import org.springframework.boot.test.context.SpringBootTest; |
| 19 | +import org.springframework.boot.test.context.TestConfiguration; |
| 20 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 21 | +import org.springframework.context.annotation.Bean; |
| 22 | +import org.springframework.context.annotation.Import; |
17 | 23 | import org.springframework.http.HttpHeaders; |
18 | 24 | import org.springframework.http.HttpStatus; |
19 | 25 | import org.springframework.http.MediaType; |
20 | | -import org.springframework.test.context.DynamicPropertyRegistry; |
21 | | -import org.springframework.test.context.DynamicPropertySource; |
22 | 26 | import org.springframework.test.web.reactive.server.EntityExchangeResult; |
23 | 27 | import org.springframework.test.web.reactive.server.WebTestClient; |
24 | 28 | import org.springframework.web.reactive.function.BodyInserters; |
25 | 29 | import org.testcontainers.containers.GenericContainer; |
26 | 30 | import org.testcontainers.containers.PostgreSQLContainer; |
27 | | -import org.testcontainers.junit.jupiter.Container; |
28 | 31 | import org.testcontainers.junit.jupiter.Testcontainers; |
29 | 32 | import org.testcontainers.utility.DockerImageName; |
30 | 33 |
|
|
34 | 37 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
35 | 38 |
|
36 | 39 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 40 | +@Import(ApplicationTest.TestConfig.class) |
37 | 41 | @AutoConfigureWebTestClient |
38 | 42 | @Testcontainers |
39 | 43 | @TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
40 | 44 | class ApplicationTest { |
41 | 45 |
|
42 | | - @Container |
43 | | - private static final PostgreSQLContainer<?> POSTGRES_CONTAINER = |
44 | | - new PostgreSQLContainer<>(DockerImageName.parse("postgres")); |
45 | | - |
46 | | - @Container |
47 | | - private static final GenericContainer<?> REDIS_CONTAINER = |
48 | | - new GenericContainer<>(DockerImageName.parse("redis")) |
49 | | - .withExposedPorts(6379); |
50 | | - |
51 | | - @DynamicPropertySource |
52 | | - private static void setApplicationProperties(DynamicPropertyRegistry registry) { |
53 | | - registry.add("spring.datasource.url", POSTGRES_CONTAINER::getJdbcUrl); |
54 | | - registry.add("spring.datasource.username", POSTGRES_CONTAINER::getUsername); |
55 | | - registry.add("spring.datasource.password", POSTGRES_CONTAINER::getPassword); |
56 | | - registry.add("spring.redis.host", REDIS_CONTAINER::getHost); |
57 | | - registry.add("spring.redis.port", REDIS_CONTAINER::getFirstMappedPort); |
58 | | - } |
59 | | - |
60 | 46 | private static PersonRequest personRequest; |
61 | 47 | private static UUID personId; |
62 | 48 |
|
@@ -184,4 +170,21 @@ private UUID getPersonIdFromLocationHeader(EntityExchangeResult<Void> exchangeRe |
184 | 170 |
|
185 | 171 | return UUID.fromString(lastSegment); |
186 | 172 | } |
| 173 | + |
| 174 | + @TestConfiguration |
| 175 | + static class TestConfig { |
| 176 | + |
| 177 | + @Bean |
| 178 | + @ServiceConnection(type = JdbcConnectionDetails.class) |
| 179 | + PostgreSQLContainer<?> POSTGRES_CONTAINER() { |
| 180 | + return new PostgreSQLContainer<>(DockerImageName.parse("postgres")); |
| 181 | + } |
| 182 | + |
| 183 | + @Bean |
| 184 | + @ServiceConnection(name = "redis", type = RedisConnectionDetails.class) |
| 185 | + GenericContainer<?> REDIS_CONTAINER() { |
| 186 | + return new GenericContainer<>(DockerImageName.parse("redis")) |
| 187 | + .withExposedPorts(6379); |
| 188 | + } |
| 189 | + } |
187 | 190 | } |
0 commit comments