From 874a5e324a9350e0c8f4ae3d883600b67acdd631 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Tue, 2 Oct 2018 13:49:41 +0200 Subject: [PATCH] #418 - Replaced external Postgres DB with one created through TestContainers. --- jdbc/r2dbc/pom.xml | 6 +++++ .../basics/InfrastructureConfiguration.java | 23 +++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/jdbc/r2dbc/pom.xml b/jdbc/r2dbc/pom.xml index a85f9c91..0848378f 100644 --- a/jdbc/r2dbc/pom.xml +++ b/jdbc/r2dbc/pom.xml @@ -38,6 +38,12 @@ test + + org.testcontainers + postgresql + 1.9.1 + + io.projectreactor reactor-core diff --git a/jdbc/r2dbc/src/test/java/example/springdata/r2dbc/basics/InfrastructureConfiguration.java b/jdbc/r2dbc/src/test/java/example/springdata/r2dbc/basics/InfrastructureConfiguration.java index 7c24b7e6..94e29bd6 100644 --- a/jdbc/r2dbc/src/test/java/example/springdata/r2dbc/basics/InfrastructureConfiguration.java +++ b/jdbc/r2dbc/src/test/java/example/springdata/r2dbc/basics/InfrastructureConfiguration.java @@ -24,6 +24,9 @@ import org.springframework.context.annotation.Configuration; import org.springframework.data.r2dbc.function.DatabaseClient; import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory; import org.springframework.data.relational.core.mapping.RelationalMappingContext; +import org.testcontainers.containers.PostgreSQLContainer; + +import javax.annotation.PreDestroy; /** * @author Oliver Gierke @@ -31,6 +34,8 @@ import org.springframework.data.relational.core.mapping.RelationalMappingContext @Configuration class InfrastructureConfiguration { + private PostgreSQLContainer postgres = new PostgreSQLContainer(); + @Bean CustomerRepository customerRepository(R2dbcRepositoryFactory factory) { return factory.getRepository(CustomerRepository.class); @@ -56,14 +61,22 @@ class InfrastructureConfiguration { @Bean PostgresqlConnectionFactory connectionFactory() { + + postgres.start(); + PostgresqlConnectionConfiguration config = PostgresqlConnectionConfiguration.builder() // - .host("localhost") // - .port(5432) // - .database("postgres") // - .username("postgres") // - .password("") // + .host(postgres.getContainerIpAddress()) // + .port(postgres.getFirstMappedPort()) // + .database(postgres.getDatabaseName()) // + .username(postgres.getUsername()) // + .password(postgres.getPassword()) // .build(); return new PostgresqlConnectionFactory(config); } + + @PreDestroy + void shutdown() { + postgres.stop(); + } }