composite-build-usage-example

Форк
0
45 строк · 1.7 Кб
1
package by.andd3dfx.templateapp;
2

3
import org.springframework.context.ApplicationContextInitializer;
4
import org.springframework.context.ConfigurableApplicationContext;
5
import org.springframework.core.env.ConfigurableEnvironment;
6
import org.springframework.core.env.MapPropertySource;
7
import org.testcontainers.containers.PostgreSQLContainer;
8
import org.testcontainers.lifecycle.Startables;
9

10
import java.util.Map;
11
import java.util.stream.Stream;
12

13
/**
14
 * According to https://reflectoring.io/spring-boot-flyway-testcontainers/
15
 */
16
public class IntegrationTestInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
17

18
    private static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine");
19

20
    @Override
21
    public void initialize(ConfigurableApplicationContext applicationContext) {
22
        startContainers();
23
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
24

25
        MapPropertySource propertySource = new MapPropertySource(
26
                "test-containers", (Map) createConnectionConfiguration()
27
        );
28
        environment.getPropertySources().addFirst(propertySource);
29
    }
30

31
    private static void startContainers() {
32
        Startables.deepStart(Stream.of(postgres)).join();
33
        // we can add further containers
34
        // here like rabbitmq or other databases
35
    }
36

37
    private static Map<String, String> createConnectionConfiguration() {
38
        return Map.of(
39
                "DB_URL", postgres.getJdbcUrl(),
40
                "DB_USERNAME", postgres.getUsername(),
41
                "DB_PASSWORD", postgres.getPassword(),
42
                "DRIVER_CLASS_NAME", postgres.getDriverClassName()
43
        );
44
    }
45
}
46

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.