【问题标题】:Reliable libraries out there for Spring boot redis integration tests用于 Spring boot redis 集成测试的可靠库
【发布时间】:2018-10-31 13:22:35
【问题描述】:

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

所以基本上我有一个标准的 spring boot 应用程序 - 我有一个单元测试 redis 缓存配置。我要做的是运行应用程序上下文自动装配一些 spring 配置并在可能的情况下针对嵌入式 redis 缓存进行测试。

我最近的一次是https://github.com/kstyrc/embedded-redis

问题在于缺乏强大的日志记录使其难以运行 - 它在本地工作,但是当我将它推上去时,Unix 服务器构建机器,它失败并且不知道为什么。

如果有人知道如何以这种方式运行集成测试,那就太好了。

谢谢,

斯蒂芬

【问题讨论】:

  • 您下载的 JAR 仅包含 Windows EXE。项目主页上的自述文件中有说明如何使用其他 OS 可执行文件。

标签: spring-boot redis integration-testing


【解决方案1】:

我正在使用 embedded-redis 进行与 redisson java 客户端的集成测试。 这是我的依赖项

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

【讨论】:

    猜你喜欢
    • 2020-02-25
    • 1970-01-01
    • 2019-02-25
    • 2015-03-23
    • 1970-01-01
    • 2015-08-20
    • 2020-04-24
    • 2017-10-27
    • 1970-01-01
    相关资源
    最近更新 更多