Keva as Embedded Redis Server for Spring Boot Test

Keva as Embedded Redis Server for Spring Boot Test


Spring Data Redis provides an easy way to integrate with Redis instances.

However, in some cases, it’s more convenient to use an embedded server than to create an environment with a real server.

In this article, we will introduce how to use Keva as an embedded Redis server for Spring Boot test.

Install Keva as a dependency

Keva is a Java library, so we can use it as a dependency in our project.


dependencies {
    implementation 'dev.keva:kevadb:1.0.0-rc2'





After adding the dependencies, we should define the connection settings between the Redis server and our application.

Let’s begin by creating a class that will hold our properties:

public class RedisProperties {
    private int redisPort;
    private String redisHost;

    public RedisProperties(
      @Value("${spring.redis.port}") int redisPort, 
      @Value("${spring.redis.host}") String redisHost) {
        this.redisPort = redisPort;
        this.redisHost = redisHost;

    // getters

Next, we should create a configuration class that defines the connection and uses our properties:

public class RedisConfiguration {

    public LettuceConnectionFactory redisConnectionFactory(
      RedisProperties redisProperties) {
        return new LettuceConnectionFactory(

    public RedisTemplate<?, ?> redisTemplate(LettuceConnectionFactory connectionFactory) {
        RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
        return template;

The configuration is quite simple. Additionally, it allows us to run the embedded server on a different port.

Check out our Introduction to Spring Data Redis article to learn more about the Redis with Spring Boot.

Keva as Embedded Redis Server

Now, we’ll configure the embedded server and use it in one of our tests.


After that, we’ll create a @TestConfiguration-annotated class:

public class TestRedisConfiguration {

    private KevaServer redisServer;

    public TestRedisConfiguration(RedisProperties redisProperties) {
        KevaConfig kevaConfig = KevaConfig.builder()
        this.redisServer = KevaServer.of(kevaConfig);

    public void postConstruct() {

    public void preDestroy() {

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.

Additionally, the server will stop once the context is destroyed.

Finally, let’s create a test that’ll use our TestRedisConfiguration class:

@SpringBootTest(classes = TestRedisConfiguration.class)
public class UserRepositoryIntegrationTest {

    private UserRepository userRepository;

    public void shouldSaveUser_toRedis() {
        UUID id = UUID.randomUUID();
        User user = new User(id, "name");

        User saved = userRepository.save(user);


The user has been saved to our embedded Keva server.

Additionally, we had to manually add TestRedisConfiguration to SpringBootTest. As we said earlier, the server has started before the test and stopped after.

The code for examples is available over on GitHub.