1、概述

Spring Data Redis提供了一种与Redis实例集成的简单方法。

但是,在某些情况下,使用嵌入式服务器比使用真实服务器创建开发和测试环境更方便。

因此,我们将学习如何设置和使用嵌入式Redis服务器。

2、依赖

让我们首先添加必要的依赖项:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> <dependency>
<groupId>it.ozimov</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.7.2</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

这个spring-boot-starter-test包含我们需要运行集成测试的各种依赖。

此外,embedded-redis包含我们将使用的嵌入式服务器。

3、设置

添加依赖项后,我们应该定义Redis服务器和我们的应用程序之间的连接设置。

让我们首先创建一个类来保存我们的属性:

@Configuration
public class RedisProperties {
private int redisPort;
private String redisHost; public RedisProperties(
@Value("${spring.redis.port}") int redisPort,
@Value("${spring.redis.host}") String redisHost) {
this.redisPort = redisPort;
this.redisHost = redisHost;
} // getters
}

接下来,我们应该创建一个配置类来定义连接并使用我们的属性:

@Configuration
@EnableRedisRepositories
public class RedisConfiguration { @Bean
public LettuceConnectionFactory redisConnectionFactory(
RedisProperties redisProperties) {
return new LettuceConnectionFactory(
redisProperties.getRedisHost(),
redisProperties.getRedisPort());
} @Bean
public RedisTemplate<?, ?> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
return template;
}
}

配置非常简单。这样我们的嵌入式服务器可以在其他的端口上运行。

4、嵌入式Redis服务器

现在,我们将配置嵌入式服务器并在我们的一项测试中使用它。

首先,让我们在测试的资源目录(src/test/resources)中创建一个application.properties文件:

spring.redis.host=localhost
spring.redis.port=6370

之后,我们将创建一个@TestConfiguration注解的配置类:

@TestConfiguration
public class TestRedisConfiguration { private RedisServer redisServer; public TestRedisConfiguration(RedisProperties redisProperties) {
this.redisServer = new RedisServer(redisProperties.getRedisPort());
} @PostConstruct
public void postConstruct() {
redisServer.start();
} @PreDestroy
public void preDestroy() {
redisServer.stop();
}
}

当context上下文启动,服务器就跟着启动。它根据我们在属性中定义的端口运行在我们的机器上。有了它,我们现在可以在不停止实际Redis服务器的情况下运行测试了。

理想情况下,我们希望在随机可用端口上启动它,但嵌入式Redis尚不具备此功能。我们现在可以做的是通过ServerSocket API 获取随机端口。

此外,当上下文停止,服务器也跟着停止。

服务器也可以由我们自己的可执行文件来提供:

this.redisServer = new RedisServer("/path/redis", redisProperties.getRedisPort());

此外,可执行文件可以按不同的操作系统来定义:

RedisExecProvider customProvider = RedisExecProvider.defaultProvider()
.override(OS.UNIX, "/path/unix/redis")
.override(OS.Windows, Architecture.x86_64, "/path/windows/redis")
.override(OS.MAC_OS_X, Architecture.x86_64, "/path/macosx/redis"); this.redisServer = new RedisServer(customProvider, redisProperties.getRedisPort());

最后,让我们创建一个使用TestRedisConfiguration类的测试吧:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestRedisConfiguration.class)
public class UserRepositoryIntegrationTest { @Autowired
private UserRepository userRepository; @Test
public void shouldSaveUser_toRedis() {
UUID id = UUID.randomUUID();
User user = new User(id, "name"); User saved = userRepository.save(user); assertNotNull(saved);
}
}

这样用户保存就到了我们的嵌入式Redis服务器。

此外,我们必须手动将TestRedisConfiguration添加到SpringBootTest。正如我们之前所说,服务器在测试之前启动并在测试之后停止。

5、结论

嵌入式Redis服务器是在测试环境中替换实际服务器的完美工具。我们已经看到了如何配置它以及如何在我们的测试中使用它。

更多技术干货,请访问我的个人网站https://pinmost.com,或关注公众号【码农熊猫】

嵌入式Redis服务器在Spring Boot测试中的使用的更多相关文章

  1. 【redis】5.spring boot项目中,直接在spring data jpa的Repository层使用redis +redis注解@Cacheable直接在Repository层使用,报错问题处理Null key returned for cache operation

    spring boot整合redis:http://www.cnblogs.com/sxdcgaq8080/p/8028970.html 首先,明确一下问题的场景 之前在spring boot整合re ...

  2. 在Spring Boot项目中使用Spock测试框架

    本文首发于个人网站:在Spring Boot项目中使用Spock测试框架 Spock框架是基于Groovy语言的测试框架,Groovy与Java具备良好的互操作性,因此可以在Spring Boot项目 ...

  3. 在spring boot环境中使用fastjson + redis的高速缓存技术

    因为项目需求,需要在spring boot环境中使用redis作数据缓存.之前的解决方案是参考的http://wiselyman.iteye.com/blog/2184884,具体使用的是Jackso ...

  4. Spring Boot项目中使用Mockito

    本文首发于个人网站:Spring Boot项目中使用Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试 ...

  5. 【redis】4.spring boot集成redis,实现数据缓存

    参考地址:https://spring.io/guides/gs/messaging-redis/ ================================================== ...

  6. Spring Boot 测试时的日志级别

    1.概览 该教程中,我将向你展示:如何在测试时设置spring boot 日志级别.虽然我们可以在测试通过时忽略日志,但是如果需要诊断失败的测试,选择正确的日志级别是非常重要的. 2.日志级别的重要性 ...

  7. Spring Boot项目中如何定制拦截器

    本文首发于个人网站:Spring Boot项目中如何定制拦截器 Servlet 过滤器属于Servlet API,和Spring关系不大.除了使用过滤器包装web请求,Spring MVC还提供Han ...

  8. Spring Boot项目中如何定制PropertyEditors

    本文首发于个人网站:Spring Boot项目中如何定制PropertyEditors 在Spring Boot: 定制HTTP消息转换器一文中我们学习了如何配置消息转换器用于HTTP请求和响应数据, ...

  9. Spring框架学习笔记(6)——阿里云服务器部署Spring Boot项目(jar包)

    最近接外包,需要部署服务器,便是参考了网上的几篇博文,成功在阿里云服务器成功部署了Spring Boot项目,特记下本篇笔记 Spring Boot项目打包 这里说一下部署的一些问题 1.mysql驱 ...

随机推荐

  1. 将视频插入视频:CVPR2019论文解析

    将视频插入视频:CVPR2019论文解析 Inserting Videos into Videos 论文链接: http://openaccess.thecvf.com/content_CVPR_20 ...

  2. H.265视频编码与技术全析(下)

    H.265视频编码与技术全析(下) 四.帧内预测模式 共35个(h264有9个),包括Planar,DC,33个方向模式: 除了Intra_Angular预测外,HEVC还和H.264/MPEG-4 ...

  3. python-selenium 引入包或者类的清晰写法

    #cording=gbk#一般最上面放系统自带的包或者类import os import time##第二层放第三方下载的包或者类from selenium import webdriverfrom ...

  4. springcloud-config配置异常Cannot clone or checkout repository 和 Authentication is required but no CredentialsProvider has been registered解决过程

    Cannot clone or checkout repository, 出现这个异常,通过检查是因为自己本地没有配置 ssh,所以配置了, https://blog.csdn.net/zy_2818 ...

  5. 操作系统-Linux命令

    一.目录结构 #因为根目录与开机有关,开机过程中仅有根目录会被挂载, 因此根目录下与开机过程有关的目录(以下5个),不能与根目录放到不同的分区去. /etc:配置文件 /dev:所需要的装置文件 /l ...

  6. 导出 Excel 模板自动生成规则,避免用户来回修改

    一句话总结 Excel 导出.导入时,根据注解自动添加单元格验证规则,避免用户因填写错误的枚举字段而反复修改 Excel 需求背景 对于 Java Web 项目,总是不可避免的出现 Excel 导入. ...

  7. Redis 面霸篇:高频问题横扫核心知识点

    「码哥字节」从高频面试问题跟大家一起横扫 Redis 核心知识点,从根本上理解 Redis ,不做八股文的工具人,做扭转乾坤的大神. 码哥到如今已经写了 9 篇 Redis 连载,后台有小伙伴也让我写 ...

  8. 【模板】 RMQ求区间最值

    RMQ RMQ简单来说就是求区间的最大值(最小值) 核心算法:动态规划 RMQ(以下以求最大值为例) F[i,j]表示 从 i 开始 到i+2j -1这个区间中的最大值 状态转移方程 F[i,j]=m ...

  9. 同事内推的那位Linux C/C++后端开发同学面试没过......

    最近同事内推了一位 Linux C/C++ 后端开发的同学到我们公司面试,我是一面的面试官,很遗憾这位工作了两年的同学面试表现不是很好.我问了如下一些问题: "redis持久化机制,redi ...

  10. redis实现分布式锁天然的缺陷

    redis分布式锁基本原理 采用 redis 实现分布式锁,主要是利用其单线程命令执行的特性,一般是 setnx, 只会有一个线程会执行成功,也就是只有一个线程能成功获取锁: 看着很完美 看看可能有什 ...