Skip to content

Commit 5ca77a9

Browse files
committed
add blog post
1 parent 7df6ef3 commit 5ca77a9

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
slug: keva-as-redis-embedded-server-spring-boot-test
3+
title: Keva as Embedded Redis Server for Spring Boot Test
4+
authors: [tu]
5+
tags: [test]
6+
---
7+
8+
Spring Data Redis provides an easy way to integrate with Redis instances.
9+
10+
However, in some cases, it's more convenient to use an embedded server than to create an environment with a real server.
11+
12+
In this article, we will introduce how to use Keva as an embedded Redis server for Spring Boot test.
13+
14+
## Install Keva as a dependency
15+
16+
Keva is a Java library, so we can use it as a dependency in our project.
17+
18+
`build.gradle`
19+
20+
```groovy
21+
dependencies {
22+
implementation 'dev.keva:kevadb:1.0.0-rc2'
23+
}
24+
```
25+
26+
or:
27+
28+
`pom.xml`
29+
30+
```xml
31+
<dependency>
32+
<groupId>dev.keva</groupId>
33+
<artifactId>kevadb</artifactId>
34+
<version>1.0.0-rc2</version>
35+
</dependency>
36+
```
37+
38+
## Setup
39+
40+
After adding the dependencies, we should define the connection settings between the Redis server and our application.
41+
42+
Let's begin by creating a class that will hold our properties:
43+
44+
```java
45+
@Configuration
46+
public class RedisProperties {
47+
private int redisPort;
48+
private String redisHost;
49+
50+
public RedisProperties(
51+
@Value("${spring.redis.port}") int redisPort,
52+
@Value("${spring.redis.host}") String redisHost) {
53+
this.redisPort = redisPort;
54+
this.redisHost = redisHost;
55+
}
56+
57+
// getters
58+
}
59+
```
60+
61+
Next, we should create a configuration class that defines the connection and uses our properties:
62+
63+
```java
64+
@Configuration
65+
@EnableRedisRepositories
66+
public class RedisConfiguration {
67+
68+
@Bean
69+
public LettuceConnectionFactory redisConnectionFactory(
70+
RedisProperties redisProperties) {
71+
return new LettuceConnectionFactory(
72+
redisProperties.getRedisHost(),
73+
redisProperties.getRedisPort());
74+
}
75+
76+
@Bean
77+
public RedisTemplate<?, ?> redisTemplate(LettuceConnectionFactory connectionFactory) {
78+
RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
79+
template.setConnectionFactory(connectionFactory);
80+
return template;
81+
}
82+
}
83+
```
84+
85+
The configuration is quite simple. Additionally, it allows us to run the embedded server on a different port.
86+
87+
Check out our Introduction to Spring Data Redis article to learn more about the Redis with Spring Boot.
88+
89+
## Keva as Embedded Redis Server
90+
91+
Now, we'll configure the embedded server and use it in one of our tests.
92+
93+
```properties
94+
spring.redis.host=localhost
95+
spring.redis.port=6370
96+
```
97+
98+
After that, we'll create a @TestConfiguration-annotated class:
99+
100+
```java
101+
@TestConfiguration
102+
public class TestRedisConfiguration {
103+
104+
private KevaServer redisServer;
105+
106+
public TestRedisConfiguration(RedisProperties redisProperties) {
107+
KevaConfig kevaConfig = KevaConfig.builder()
108+
.hostname(redisProperties.getRedisHost())
109+
.port(redisProperties.getRedisPort())
110+
.persistence(false)
111+
.aof(false)
112+
.workDirectory("./")
113+
.build();
114+
this.redisServer = KevaServer.of(kevaConfig);
115+
}
116+
117+
@PostConstruct
118+
public void postConstruct() {
119+
redisServer.run();
120+
}
121+
122+
@PreDestroy
123+
public void preDestroy() {
124+
redisServer.shutdown();
125+
}
126+
}
127+
```
128+
129+
The server will start once the context is up. It'll start on our machine on the port that we've defined in our properties. For instance, we can now run the test without stopping the actual Redis server.
130+
131+
Additionally, the server will stop once the context is destroyed.
132+
133+
Finally, let's create a test that'll use our TestRedisConfiguration class:
134+
135+
```java
136+
@RunWith(SpringRunner.class)
137+
@SpringBootTest(classes = TestRedisConfiguration.class)
138+
public class UserRepositoryIntegrationTest {
139+
140+
@Autowired
141+
private UserRepository userRepository;
142+
143+
@Test
144+
public void shouldSaveUser_toRedis() {
145+
UUID id = UUID.randomUUID();
146+
User user = new User(id, "name");
147+
148+
User saved = userRepository.save(user);
149+
150+
assertNotNull(saved);
151+
}
152+
}
153+
```
154+
155+
The user has been saved to our embedded Keva server.
156+
157+
Additionally, we had to manually add TestRedisConfiguration to SpringBootTest. As we said earlier, the server has started before the test and stopped after.
158+
159+
[Reference](https://www.baeldung.com/spring-embedded-redis)

0 commit comments

Comments
 (0)