Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jupiter-tools/spring-test-redis

Tools to write integration tests of Spring Framework with Redis
https://github.com/jupiter-tools/spring-test-redis

redis spring-boot spring-boot-redis spring-boot-test spring-data-redis spring-redis testcontainers

Last synced: 2 months ago
JSON representation

Tools to write integration tests of Spring Framework with Redis

Awesome Lists containing this project

README

        

:toc: preamble

# Spring Test Redis

image:https://travis-ci.com/jupiter-tools/spring-test-redis.svg?branch=master["Build Status", link="https://travis-ci.com/jupiter-tools/spring-test-redis"]

Tools to write integration tests of Spring Framework with Redis.

## Overview

image:./images/redis.png[redis container scheme,300]

## How to write integration tests on Spring Framework with Redis

Add this library in dependencies:

[source,xml]
----

com.jupiter-tools
spring-test-redis
0.1

----

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

[source, java]
----
@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:

[source, java]
----
@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:

image:./images/multiple_containers.png[multiple docker container result in stdout]