workshop
  • Introduction
  • Step 1: Getting Started
  • Step 2: Exploring the app
  • Step 3: Adding some tests
  • Step 4: Your first Testcontainers integration
  • Step 5: Hello, r u 200 OK?
  • Step 6: Adding Redis
  • Step 7: Test the API
  • Step 8: Edge cases
  • Step 9: Data initialization strategies
  • Step 10: Migrating from Docker Compose
Powered by GitBook
On this page

Step 6: Adding Redis

PreviousStep 5: Hello, r u 200 OK?NextStep 7: Test the API

Last updated 3 years ago

The simplest way to provide a Redis instance for your tests is to use GenericContainer with a Redis Docker image: The integration between the tests code and Testcontainers is straightforward.

Rules? No thanks!

Testcontainers comes with first class support for JUnit, but in our app we want to have a single Redis instance shared between all tests. Luckily, there are the .start()/.stop() methods of GenericContainer to start or stop it manually.

Just add the following code to your AbstractIntegrationTest with the following code:

static final GenericContainer redis = new GenericContainer("redis:6-alpine")
                                            .withExposedPorts(6379);

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

Simple and beautiful, huh?

Run the tests, now they should all pass.

https://www.testcontainers.org/usage/generic_containers.html