I'm doing integration tests in my Spring Boot app. The app needs a Redis to work with.
During development phase, I have a local container of Redis that the app connects to.
For the integration tests, I'm using testcontainers and I also followed their example of how to use a Redis container.
At some point I understood that the test run correctly only when the development container was up and running. If it is down, the integration tests are falling because they can't reach Redis.
So the integration test class looks like that:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SharkApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations = "classpath:application-integrationtests.yml")
@AutoConfigureMockMvc
public class SharkIntegrationTest {
static GenericContainer redis = new GenericContainer("redis:3.0.6")
.withExposedPorts(6379);
@BeforeClass
public static void before(){
redis.start();
}
@AfterClass
public static void after(){
redis.stop();
}
...
When running the test, I can see this in the log:
14:36:24.372 [main] DEBUG [redis:3.0.6] - Starting container: redis:3.0.6
14:36:24.372 [main] DEBUG [redis:3.0.6] - Trying to start container:
redis:3.0.6
14:36:24.373 [main] DEBUG [redis:3.0.6] - Trying to start container:
redis:3.0.6 (attempt 1/1)
14:36:24.373 [main] DEBUG [redis:3.0.6] - Starting container: redis:3.0.6
14:36:24.373 [main] INFO [redis:3.0.6] - Creating container for image:
redis:3.0.6
...
14:36:25.282 [main] INFO [redis:3.0.6] - Container redis:3.0.6 started
But then the app fails as it can't reach Redis:
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
At some point I tried to change the port on which the container should start. From 6379 to 16379 (changed both in the code and in the yml file), but then the test is entering an endless loop and prints to the screen:
14:41:57.258 [ducttape-0] DEBUG org.testcontainers.containers.ExecInContainerPattern - /amazing_beaver: Running "exec" command: /bin/bash -c </dev/tcp/localhost/16379 && echo