Spring 启动 redis 集成测试的可靠库
Reliable libraries out there for Spring boot redis integration tests
这更像是一个工具问题 - 谷歌搜索我真的没有太多运气。
所以基本上我有一个标准的 spring 启动应用程序 - 我有一个单元测试 redis 缓存配置。我想要做的是 运行 应用程序上下文自动装配一些 spring 配置并在可能的情况下针对嵌入式 redis 缓存进行测试。
我最近的是这个https://github.com/kstyrc/embedded-redis。
问题在于缺乏可靠的日志记录使得 运行 变得困难 - 它在本地工作,但是当我将它推送到 Unix 服务器构建机器时,它失败了,不知道为什么。
如果有人知道如何 运行 以这种方式进行集成测试,那就太好了。
谢谢,
斯蒂芬
我正在使用 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'
在 class 之前启动嵌入式 Redis 服务器并在 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 启动应用程序 - 我有一个单元测试 redis 缓存配置。我想要做的是 运行 应用程序上下文自动装配一些 spring 配置并在可能的情况下针对嵌入式 redis 缓存进行测试。
我最近的是这个https://github.com/kstyrc/embedded-redis。
问题在于缺乏可靠的日志记录使得 运行 变得困难 - 它在本地工作,但是当我将它推送到 Unix 服务器构建机器时,它失败了,不知道为什么。
如果有人知道如何 运行 以这种方式进行集成测试,那就太好了。
谢谢,
斯蒂芬
我正在使用 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'
在 class 之前启动嵌入式 Redis 服务器并在 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);
}
}