Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
package com.example.solidconnection.support;

import jakarta.annotation.PostConstruct;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;

@TestConfiguration
public class RedisTestContainer {
public class RedisTestContainer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

@Container
private static final GenericContainer<?> CONTAINER = new GenericContainer<>("redis:7.0");
private static final int ORIGINAL_PORT = 6379;
private static final GenericContainer<?> CONTAINER = new GenericContainer<>("redis:7.0")
.withExposedPorts(ORIGINAL_PORT);

@DynamicPropertySource
static void redisProperties(DynamicPropertyRegistry registry) {
registry.add("spring.redis.host", CONTAINER::getHost);
registry.add("spring.redis.port", CONTAINER::getFirstMappedPort);
static {
CONTAINER.start();
}

@PostConstruct
void startContainer() {
if (!CONTAINER.isRunning()) {
CONTAINER.start();
}
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
Comment on lines +18 to +19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 변경하면서 redis와 mysql testcontainers 구현 방식이 달라졌는데 통일하는 건 어떨까요?

Copy link
Collaborator Author

@nayonsoso nayonsoso Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굳이 다르게 설정할 필요는 없겠네요.
반영했습니다! 🫡

c364691

TestPropertyValues.of(
"spring.data.redis.host=" + CONTAINER.getHost(),
"spring.data.redis.port=" + CONTAINER.getMappedPort(ORIGINAL_PORT)
).applyTo(applicationContext.getEnvironment());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.lang.annotation.ElementType;
Expand All @@ -12,10 +13,11 @@
import java.lang.annotation.Target;

@ExtendWith({DatabaseClearExtension.class})
@ContextConfiguration(initializers = RedisTestContainer.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
@Import({MySQLTestContainer.class, RedisTestContainer.class})
@Import({MySQLTestContainer.class})
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestContainerSpringBootTest {
Expand Down