-
Notifications
You must be signed in to change notification settings - Fork 6
/
BaseIntegrationTest.java
64 lines (54 loc) · 2.3 KB
/
BaseIntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.example.podman.testcontainers;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;
@SpringBootTest
public abstract class BaseIntegrationTest {
public static final int REDIS_PORT = 6379;
private static final PostgreSQLContainer<?> POSTGRES;
private static final GenericContainer<?> REDIS;
static {
POSTGRES = createPostgresContainer();
REDIS = createRedisContainer();
}
private static PostgreSQLContainer<?> createPostgresContainer() {
PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>(DockerImageName.parse("postgres:14-alpine"));
postgres.start();
Runtime.getRuntime().addShutdownHook(new Thread(postgres::stop));
return postgres;
}
private static GenericContainer<?> createRedisContainer() {
GenericContainer<?> redis =
new GenericContainer<>(DockerImageName.parse("redis:6-alpine"))
.withExposedPorts(REDIS_PORT);
redis.start();
Runtime.getRuntime().addShutdownHook(new Thread(redis::stop));
return redis;
}
@DynamicPropertySource
static void registerPostgresProperties(DynamicPropertyRegistry registry) {
registry.add("spring.r2dbc.url", BaseIntegrationTest::getPostgresR2bcUrl);
registry.add("spring.r2dbc.username", POSTGRES::getUsername);
registry.add("spring.r2dbc.password", POSTGRES::getPassword);
registry.add("spring.flyway.url", POSTGRES::getJdbcUrl);
registry.add("spring.flyway.user", POSTGRES::getUsername);
registry.add("spring.flyway.password", POSTGRES::getPassword);
}
@DynamicPropertySource
static void registerRedisProperties(DynamicPropertyRegistry registry) {
registry.add("spring.redis.host", REDIS::getContainerIpAddress);
registry.add("spring.redis.port", () -> REDIS.getMappedPort(REDIS_PORT));
}
private static String getPostgresR2bcUrl() {
return "r2dbc:postgresql://"
+ POSTGRES.getContainerIpAddress()
+ ":"
+ POSTGRES.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT)
+ "/"
+ POSTGRES.getDatabaseName();
}
}