Skip to content

Commit

Permalink
polish code
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 1, 2024
1 parent b1a9972 commit ea9ac64
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
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;
import org.testcontainers.containers.MySQLContainer;
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);
Expand All @@ -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();
}
}
}

0 comments on commit ea9ac64

Please sign in to comment.