diff --git a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java index f3fdfadaa..ebee06210 100644 --- a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java +++ b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java @@ -13,7 +13,9 @@ import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = ContainersConfiguration.class) +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = {ContainersConfiguration.class, MultipleDataSourcesApplication.class}) @AutoConfigureMockMvc class ApplicationIntegrationTest { diff --git a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java index bc715ca4b..61aef1a2a 100644 --- a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java +++ b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java @@ -1,5 +1,6 @@ package com.example.multipledatasources.common; +import jakarta.annotation.PreDestroy; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.test.context.DynamicPropertyRegistrar; @@ -7,32 +8,35 @@ import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.utility.DockerImageName; +/** + * Test configuration class that manages MySQL and PostgreSQL containers for integration testing. + * This class is responsible for starting the containers, registering their connection properties, + * and ensuring proper cleanup on shutdown. + */ @TestConfiguration(proxyBeanMethods = false) public class ContainersConfiguration { + private MySQLContainer mySQLContainer; + private PostgreSQLContainer postgreSQLContainer; + @Bean MySQLContainer mySQLContainer() { - MySQLContainer mysql = - new MySQLContainer<>(DockerImageName.parse("mysql").withTag("9.1")); - mysql.start(); - return mysql; + mySQLContainer = new MySQLContainer<>(DockerImageName.parse("mysql").withTag("9.1")); + mySQLContainer.start(); + return mySQLContainer; } @Bean PostgreSQLContainer postgreSQLContainer() { - PostgreSQLContainer postgres = + postgreSQLContainer = new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("17.0-alpine")); - postgres.start(); - return postgres; + postgreSQLContainer.start(); + return postgreSQLContainer; } @Bean - public DynamicPropertyRegistrar kafkaProperties( + public DynamicPropertyRegistrar databaseProperties( MySQLContainer mySQLContainer, PostgreSQLContainer postgreSQLContainer) { - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - mySQLContainer.stop(); - postgreSQLContainer.stop(); - })); return (properties) -> { // Connect our Spring application to our Testcontainers instances properties.add("app.datasource.cardholder.url", mySQLContainer::getJdbcUrl); @@ -43,4 +47,14 @@ public DynamicPropertyRegistrar kafkaProperties( properties.add("app.datasource.member.password", postgreSQLContainer::getPassword); }; } + + @PreDestroy + public void cleanup() { + if (mySQLContainer != null && mySQLContainer.isRunning()) { + mySQLContainer.stop(); + } + if (postgreSQLContainer != null && postgreSQLContainer.isRunning()) { + postgreSQLContainer.stop(); + } + } }