The Redis container does not run as a testcontainer

85 Views Asked by At

I do integration tests using Spring Boot, TestContainers, redis and Junit 5.

The code was written for the test as follows.


@Testcontainers
public class RedisTestConfig {
    private static final DockerImageName REDIS_CONTAINER_VERSION = DockerImageName.parse("redis:5.0.3-alpine");

    @Container
    public static GenericContainer<?> REDIS_CONTAINER = new GenericContainer<>(REDIS_CONTAINER_VERSION)
            .withExposedPorts(6379);

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

The test code is calling the following to use it.

@Import(RedisTestConfig.class)
@SpringBootTest
@Transactional
class OAuthAuthenticationFilterTest {
  // do test..
}

However, the following error occurs and the test fails.

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis
io.lettuce.core.RedisConnectionException: Unable to connect to localhost/<unresolved>:6379
java.net.ConnectException: Connection refused

Only the following logs appear for testcontainer.

2024-02-27T01:17:22.548+0900 [DEBUG] [TestEventLogger]     2024-02-27T01:17:22.548+09:00  INFO 41385 --- [    Test worker] org.testcontainers.images.PullPolicy     : Image pull policy will be performed by: DefaultPullPolicy()

I don't know what part to check to solve it. Please check if the problem is caused by the wrong configuration.

0

There are 0 best solutions below