Skip to content
Tools to write integration tests of Spring Framework with Redis
Java
Branch: master
Clone or download
Latest commit 4810207 Jul 12, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
images change the System.out.println on logger May 25, 2019
src Merge pull request #4 from jupiter-tools/feature/add-a-readme May 25, 2019
.gitignore draft version of multiple redis containers May 24, 2019
.travis.yml openjdk Jul 12, 2019
README.adoc improve the documentation May 25, 2019
pom.xml

README.adoc

Spring Test Redis

Build Status

Tools to write integration tests of Spring Framework with Redis.

Overview

redis container scheme

How to write integration tests on Spring Framework with Redis

Add this library in dependencies:

<dependency>
    <groupId>com.jupiter-tools</groupId>
    <artifactId>spring-test-redis</artifactId>
    <version>0.1</version>
</dependency>

And now, you can start Redis in docker (TestContainers) by the using of @RedisTestContainer annotation in tests:

@SpringBootTest
@RedisTestContainer
class RedisTestContainerTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void readWriteValueByRedisTemplate() {
        String key = "test";
        String value = "sabracadabra";
        // Act
        redisTemplate.opsForValue().set(key, value);
        // Assert
        assertThat(redisTemplate.opsForValue().get(key)).isEqualTo(value);
    }
}

You can use this annotation to start Redis container in tests both with JUnit5 and JUnit4. The implementation doesn’t depend on some test framework, just on the Spring Framework.

How to use multiple Redis containers in the one test case:

@SpringBootTest
@RedisTestContainer  (1)
@RedisTestContainer(hostTargetProperty = "my.host", portTargetProperty = "my.port")  (2)
class MultipleContainerInOneTest {

    ...

}
  1. start a Redis test container and set a host/port value of started container to default Spring Boot properties (spring.redis.host and spring.redis.port)

  2. start another Redis container and set a host/port value to specified properties, exactly in these properties you can read an actual value of host/port after run application context.

After test execution you can see something like this in the output:

multiple docker container result in stdout

You can’t perform that action at this time.