From f0ea3f714ae1b9e08e96e35b7642e840f5030e42 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Fri, 10 May 2024 13:24:35 +0000 Subject: [PATCH] feat : use testcontainers in dev mode --- jpa/multitenancy/partition/pom.xml | 6 ++-- .../partition/TestApplication.java | 22 ++++++++++++++ .../common/AbstractIntegrationTest.java | 5 ++-- .../common/DBContainerInitializer.java | 29 ------------------- 4 files changed, 27 insertions(+), 35 deletions(-) create mode 100644 jpa/multitenancy/partition/src/test/java/com/example/multitenancy/partition/TestApplication.java delete mode 100644 jpa/multitenancy/partition/src/test/java/com/example/multitenancy/partition/common/DBContainerInitializer.java diff --git a/jpa/multitenancy/partition/pom.xml b/jpa/multitenancy/partition/pom.xml index b84ea0a59..6db8b2ad9 100644 --- a/jpa/multitenancy/partition/pom.xml +++ b/jpa/multitenancy/partition/pom.xml @@ -89,8 +89,8 @@ test - org.awaitility - awaitility + org.springframework.boot + spring-boot-testcontainers test @@ -234,7 +234,7 @@ - 1.17.0 + 1.19.2 diff --git a/jpa/multitenancy/partition/src/test/java/com/example/multitenancy/partition/TestApplication.java b/jpa/multitenancy/partition/src/test/java/com/example/multitenancy/partition/TestApplication.java new file mode 100644 index 000000000..d50968e82 --- /dev/null +++ b/jpa/multitenancy/partition/src/test/java/com/example/multitenancy/partition/TestApplication.java @@ -0,0 +1,22 @@ +package com.example.multitenancy.partition; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.context.annotation.Bean; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.utility.DockerImageName; + +@TestConfiguration(proxyBeanMethods = false) +public class TestApplication { + + @Bean + @ServiceConnection + PostgreSQLContainer postgreSQLContainer() { + return new PostgreSQLContainer<>(DockerImageName.parse("postgres:16.3-alpine")); + } + + public static void main(String[] args) { + SpringApplication.from(Application::main).with(TestApplication.class).run(args); + } +} diff --git a/jpa/multitenancy/partition/src/test/java/com/example/multitenancy/partition/common/AbstractIntegrationTest.java b/jpa/multitenancy/partition/src/test/java/com/example/multitenancy/partition/common/AbstractIntegrationTest.java index 8306b0ae9..5971336da 100644 --- a/jpa/multitenancy/partition/src/test/java/com/example/multitenancy/partition/common/AbstractIntegrationTest.java +++ b/jpa/multitenancy/partition/src/test/java/com/example/multitenancy/partition/common/AbstractIntegrationTest.java @@ -3,17 +3,16 @@ import static com.example.multitenancy.partition.utils.AppConstants.PROFILE_TEST; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; +import com.example.multitenancy.partition.TestApplication; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.web.servlet.MockMvc; @ActiveProfiles({PROFILE_TEST}) -@SpringBootTest(webEnvironment = RANDOM_PORT) -@ContextConfiguration(initializers = {DBContainerInitializer.class}) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = TestApplication.class) @AutoConfigureMockMvc public abstract class AbstractIntegrationTest { diff --git a/jpa/multitenancy/partition/src/test/java/com/example/multitenancy/partition/common/DBContainerInitializer.java b/jpa/multitenancy/partition/src/test/java/com/example/multitenancy/partition/common/DBContainerInitializer.java deleted file mode 100644 index b2e71318b..000000000 --- a/jpa/multitenancy/partition/src/test/java/com/example/multitenancy/partition/common/DBContainerInitializer.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.example.multitenancy.partition.common; - -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.utility.DockerImageName; - -public class DBContainerInitializer - implements ApplicationContextInitializer { - - private static final PostgreSQLContainer sqlContainer = - new PostgreSQLContainer<>(DockerImageName.parse("postgres:16.3-alpine")) - .withDatabaseName("integration-tests-db") - .withUsername("username") - .withPassword("password"); - - static { - sqlContainer.start(); - } - - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - TestPropertyValues.of( - "spring.datasource.url=" + sqlContainer.getJdbcUrl(), - "spring.datasource.username=" + sqlContainer.getUsername(), - "spring.datasource.password=" + sqlContainer.getPassword()) - .applyTo(configurableApplicationContext.getEnvironment()); - } -}