aspiringCoder
aspiringCoder

Reputation: 415

Reliable libraries out there for Spring boot redis integration tests

This is more of a question for a tool - googling around I haven't really had much luck.

So basically I have a standard spring boot app - and I have a unit test redis cache configuration. What I am looking to do is run the app context autowire some spring configs and test against a embedded redis cache if possible.

Closest I have come is this https://github.com/kstyrc/embedded-redis.

Problem with that is the lack of robust logging is making it difficult to run - its working locally, but when I push it up, Unix server build machine, its failing and no idea why.

If anyone has any idea of how to run integration tests this way - it would be great.

thanks,

Stefan

Upvotes: 2

Views: 6218

Answers (1)

Nitin
Nitin

Reputation: 2911

I am using embedded-redis for my integration testing with redisson java client. Here is my dependency

compile group: 'org.redisson', name: 'redisson', version: '3.6.5'
testCompile group: 'it.ozimov', name: 'embedded-redis', version: '0.7.2'

Start embedded redis server before class and stop it in after class.

Redis property:

spring.redis.host=localhost
spring.redis.port=6379

Sample integration test.

import java.util.concurrent.TimeUnit;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.redisson.api.RMap;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;


import redis.embedded.RedisServer;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class RedisTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(RedisTest.class);

    private static RedisServer REDISSERVER = new RedisServer(6379);

    @LocalServerPort
    private int port;

    @Autowired
    private RedissonClient redissonClient;

    @BeforeClass
    public static final void before() {
        REDISSERVER.start();
    }

    @AfterClass
    public static  final void after() { 
        REDISSERVER.stop();
    }

    @Test
    public void testRedis() throws InterruptedException {
        //map
        RMap<String, String> map = redissonClient.getMap("user");
        map.put("name", "Redis Server");
        Assert.assertTrue(map.get("name").equals("Redis Server"));

        //mapcache
        RMapCache<String, String> mapCache = redissonClient.getMapCache("tempUser");
        mapCache.put("name", "Redis Server", 5, TimeUnit.SECONDS);
        Assert.assertTrue(mapCache.get("name").equals("Redis Server"));
        Thread.sleep(7000); //wait for 7 sec.
        Assert.assertTrue(mapCache.get("name") == null);
    }
}

Upvotes: 4

Related Questions