Skip to content

Commit 03b912b

Browse files
authored
Merge pull request #4 from jupiter-tools/feature/add-a-readme
Feature/add a readme
2 parents 0e84f3b + b1ac7f6 commit 03b912b

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

README.adoc

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
:toc: preamble
2+
3+
# Spring Test Redis
4+
5+
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"]
6+
7+
8+
Tools to write integration tests of Spring Framework with Redis.
9+
10+
## Overview
11+
12+
image:./images/redis.png[redis container scheme,300]
13+
14+
## How to write integration tests on Spring Framework with Redis
15+
16+
Add this library in dependencies:
17+
18+
[source,xml]
19+
----
20+
<dependency>
21+
<groupId>com.jupiter-tools</groupId>
22+
<artifactId>spring-test-redis</artifactId>
23+
<version>0.1</version>
24+
</dependency>
25+
----
26+
27+
And now, you can start Redis in docker (TestContainers) by the using of `@RedisTestContainer` annotation in tests:
28+
29+
[source, java]
30+
----
31+
@SpringBootTest
32+
@RedisTestContainer
33+
class RedisTestContainerTest {
34+
35+
@Autowired
36+
private RedisTemplate redisTemplate;
37+
38+
@Test
39+
void readWriteValueByRedisTemplate() {
40+
String key = "test";
41+
String value = "sabracadabra";
42+
// Act
43+
redisTemplate.opsForValue().set(key, value);
44+
// Assert
45+
assertThat(redisTemplate.opsForValue().get(key)).isEqualTo(value);
46+
}
47+
}
48+
----
49+
50+
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.
51+
52+
## How to use multiple Redis containers in the one test case:
53+
54+
[source, java]
55+
----
56+
@SpringBootTest
57+
@RedisTestContainer <1>
58+
@RedisTestContainer(hostTargetProperty = "my.host", portTargetProperty = "my.port") <2>
59+
class MultipleContainerInOneTest {
60+
61+
...
62+
63+
}
64+
----
65+
<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`)
66+
<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.
67+
68+
After test execution you can see something like this in the output:
69+
70+
image:./images/multiple_containers.png[multiple docker container result in stdout]

images/multiple_containers.png

240 KB
Loading

images/redis.png

121 KB
Loading

src/main/java/com/jupitertools/springtestredis/customizer/PropertyMutationContextCustomizer.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.jupitertools.springtestredis.customizer;
22

33
import com.jupitertools.springtestredis.RedisTestContainer;
4+
import org.apache.commons.logging.Log;
5+
import org.apache.commons.logging.LogFactory;
46
import org.springframework.context.ConfigurableApplicationContext;
57
import org.springframework.test.context.ContextCustomizer;
68
import org.springframework.test.context.MergedContextConfiguration;
@@ -24,6 +26,8 @@ public class PropertyMutationContextCustomizer implements ContextCustomizer {
2426

2527
private static final Integer REDIS_PORT = 6379;
2628

29+
private static final Log logger = LogFactory.getLog("RedisTestContainer");
30+
2731
public PropertyMutationContextCustomizer(Set<RedisContainerDescription> descriptions) {
2832
this.descriptions = descriptions;
2933
}
@@ -33,8 +37,7 @@ public void customizeContext(ConfigurableApplicationContext context,
3337
MergedContextConfiguration mergedConfig) {
3438

3539
for (RedisContainerDescription description : descriptions) {
36-
37-
System.out.println("Start REDIS TestContainer");
40+
logger.info("Start REDIS TestContainer");
3841
GenericContainer redis = new GenericContainer("redis:latest").withExposedPorts(REDIS_PORT);
3942
redis.start();
4043

@@ -48,8 +51,12 @@ public void customizeContext(ConfigurableApplicationContext context,
4851

4952
@Override
5053
public boolean equals(Object o) {
51-
if (this == o) { return true; }
52-
if (o == null || getClass() != o.getClass()) { return false; }
54+
if (this == o) {
55+
return true;
56+
}
57+
if (o == null || getClass() != o.getClass()) {
58+
return false;
59+
}
5360
PropertyMutationContextCustomizer that = (PropertyMutationContextCustomizer) o;
5461
return Objects.equals(descriptions, that.descriptions);
5562
}

0 commit comments

Comments
 (0)