Ik gebruik embedded-redis voor mijn integratietesten met redisson java-client.Hier is mijn afhankelijkheid
compile group: 'org.redisson', name: 'redisson', version: '3.6.5'
testCompile group: 'it.ozimov', name: 'embedded-redis', version: '0.7.2'
Start de ingebouwde redis-server voor de les en stop deze na de les.
Redis-eigenschap:
spring.redis.host=localhost
spring.redis.port=6379
Voorbeeld integratietest.
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);
}
}