To create a testing environment that closely resembles the production environment in Spring Boot, we used Test Containers.
While the MySQL test container worked well, we encountered an issue where the Redis container wouldn’t start.
Redis Container
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;
@SuppressWarnings("NonAsciiCharacters")
final class RedisContainerProvider {
private static final DockerImageName DOCKER_IMAGE_NAME = DockerImageName.parse("redis:7.0");
private static final Integer REDIS_PORT = 6379;
private static final GenericContainer<?> REDIS_CONTAINER;
static {
REDIS_CONTAINER = new GenericContainer<>(DOCKER_IMAGE_NAME)
.withExposedPorts(REDIS_PORT)
.waitingFor(Wait.forHealthcheck());
REDIS_CONTAINER.start();
System.setProperty("spring.data.redis.host", REDIS_CONTAINER.getHost());
System.setProperty("spring.data.redis.port", String.valueOf(REDIS_CONTAINER.getMappedPort(REDIS_PORT)));
}
@Test
void 레디스_실행_여부_확인() {
assertThat(REDIS_CONTAINER.isRunning()).isTrue();
}
}
application.yml
redis:
port: 6379
host: localhost
password: password
The exception message indicates that it’s a “Connection refused: localhost/127.0.0.1:6379.”
Exception log
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to localhost/<unresolved>:6379
at app//io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78)
at app//io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
at app//io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:350)
at app//io.lettuce.core.RedisClient.connect(RedisClient.java:216)
at app//org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.lambda$getConnection$1(StandaloneConnectionProvider.java:112)
at [email protected]/java.util.Optional.orElseGet(Optional.java:364)
at app//org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:112)
at app//org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1531)
... 166 more
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:6379
Caused by: java.net.ConnectException: Connection refused
at java.base/sun.nio.ch.Net.pollConnect(Native Method)
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:337)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:776)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:842)
The Redis container isn’t starting, so I understand that Spring is trying to create a bean using the Redis host and port specified in application.yml, which is causing a connection issue.
Even when trying to use @DynamicPropertySource, the same exception occurs.
@SuppressWarnings("NonAsciiCharacters")
final class RedisContainerProvider {
private static final DockerImageName DOCKER_IMAGE_NAME = DockerImageName.parse("redis:7.0");
private static final Integer REDIS_PORT = 6379;
private static final GenericContainer<?> REDIS_CONTAINER;
static {
REDIS_CONTAINER = new GenericContainer<>(DOCKER_IMAGE_NAME)
.withExposedPorts(REDIS_PORT)
.waitingFor(Wait.forHealthcheck());
REDIS_CONTAINER.start();
// System.setProperty("spring.data.redis.host", REDIS_CONTAINER.getHost());
// System.setProperty("spring.data.redis.port", String.valueOf(REDIS_CONTAINER.getMappedPort(REDIS_PORT)));
}
@DynamicPropertySource
private static void registerRedisProperties(DynamicPropertyRegistry registry) {
registry.add("spring.data.redis.host", REDIS_CONTAINER::getHost);
registry.add("spring.data.redis.port", () -> REDIS_CONTAINER.getMappedPort(REDIS_PORT)
.toString());
}
@Test
void 레디스_실행_여부_확인() {
assertThat(REDIS_CONTAINER.isRunning()).isTrue();
}
}
have a nice day – kevin