栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/668755.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号