可靠的库用于Spring Boot Redis集成测试 [英] Reliable libraries out there for Spring boot redis integration tests

查看:364
本文介绍了可靠的库用于Spring Boot Redis集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更多是一个工具的问题-谷歌搜索我真的没有多大运气.

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

所以基本上我有一个标准的Spring Boot应用程序-我有一个单元测试Redis缓存配置.我想做的是运行应用程序上下文自动关联一些spring配置,并在可能的情况下针对嵌入式Redis缓存进行测试.

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.

我最近来的是这个 https://github.com/kstyrc/embedded-redis.

问题在于缺少健壮的日志记录使其难以运行-它在本地运行,但是当我将其推上后,Unix服务器就在构建机器,但它失败了,不知道为什么.

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.

谢谢

Stefan

推荐答案

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

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'

在上课之前启动嵌入式Redis服务器,并在下课后停止它.

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

Redis属性:

spring.redis.host=localhost
spring.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);
    }
}

这篇关于可靠的库用于Spring Boot Redis集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆