|
1 | 1 | package uk.gov.hmcts.cp.config; |
2 | 2 |
|
3 | | -import lombok.SneakyThrows; |
4 | | -import lombok.extern.slf4j.Slf4j; |
5 | 3 | import org.springframework.boot.test.util.TestPropertyValues; |
6 | 4 | import org.springframework.context.ApplicationContextInitializer; |
7 | 5 | import org.springframework.context.ConfigurableApplicationContext; |
8 | 6 | import org.testcontainers.containers.PostgreSQLContainer; |
9 | 7 |
|
10 | | -@Slf4j |
11 | | -public class TestContainersInitialise implements ApplicationContextInitializer<ConfigurableApplicationContext> { |
| 8 | +public class TestContainersInitialise |
| 9 | + implements ApplicationContextInitializer<ConfigurableApplicationContext> { |
12 | 10 |
|
13 | | - private static final PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer( |
14 | | - "postgres") |
15 | | - .withDatabaseName("postgres") |
16 | | - .withUsername("postgres") |
17 | | - .withPassword("postgres"); |
| 11 | + private static final PostgreSQLContainer<?> postgreSQLContainer = |
| 12 | + new PostgreSQLContainer<>("postgres:15.3") |
| 13 | + .withDatabaseName("postgres") |
| 14 | + .withUsername("postgres") |
| 15 | + .withPassword("postgres") |
| 16 | + .withReuse(true); |
18 | 17 |
|
| 18 | + static { |
| 19 | + postgreSQLContainer.start(); // start once |
| 20 | + } |
19 | 21 |
|
20 | | - @SneakyThrows |
21 | 22 | @Override |
22 | | - public void initialize(ConfigurableApplicationContext applicationContext) { |
23 | | - postgreSQLContainer.start(); |
| 23 | + public void initialize(ConfigurableApplicationContext context) { |
| 24 | + |
24 | 25 | TestPropertyValues.of( |
25 | 26 | "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(), |
26 | 27 | "spring.datasource.username=" + postgreSQLContainer.getUsername(), |
27 | 28 | "spring.datasource.password=" + postgreSQLContainer.getPassword() |
28 | | - ).applyTo(applicationContext.getEnvironment()); |
29 | | - log.info("COLING starting container"); |
| 29 | + ).applyTo(context.getEnvironment()); |
30 | 30 | } |
31 | 31 | } |
32 | | - |
0 commit comments