sql >> Database teknologi >  >> NoSQL >> Redis

Pålidelige biblioteker derude til Spring boot redis integrationstest

Jeg bruger embedded-redis til min integrationstest med redisson java-klient. Her er min afhængighed

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

Start den indlejrede redis-server før undervisningen, og stop den efter undervisningen.

Redis-ejendom:

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

Eksempel integrationstest.

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);
    }
}



  1. Azure Redis Session State-fejl Timeout udfører EVAL, inst:1 , queue:2

  2. En introduktion til MongoDB Zone Basics

  3. Diagnosticerer uventet redis-serverfejl

  4. Opret et 2dsphere Geospatial Index for sfæriske forespørgsler i MongoDB