用于Spring Boot Redis集成测试的可靠库

用于Spring Boot Redis集成测试的可靠库,第1张

用于Spring Boot Redis集成测试的可靠库

我正在使用Embedded-redis与Redisson Java
Client进行集成测试。这是我的依赖

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

在上课前启动嵌入式Redis服务器,并在下课后将其停止。

Redis属性:

spring.redis.host=localhostspring.redis.port=6379

样本 集成测试

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


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5674010.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存