嵌入式Redis服务器在Spring Boot测试中的使用
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测试中的使用的更多相关文章
- 【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 ...
- 在Spring Boot项目中使用Spock测试框架
本文首发于个人网站:在Spring Boot项目中使用Spock测试框架 Spock框架是基于Groovy语言的测试框架,Groovy与Java具备良好的互操作性,因此可以在Spring Boot项目 ...
- 在spring boot环境中使用fastjson + redis的高速缓存技术
因为项目需求,需要在spring boot环境中使用redis作数据缓存.之前的解决方案是参考的http://wiselyman.iteye.com/blog/2184884,具体使用的是Jackso ...
- Spring Boot项目中使用Mockito
本文首发于个人网站:Spring Boot项目中使用Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试 ...
- 【redis】4.spring boot集成redis,实现数据缓存
参考地址:https://spring.io/guides/gs/messaging-redis/ ================================================== ...
- Spring Boot 测试时的日志级别
1.概览 该教程中,我将向你展示:如何在测试时设置spring boot 日志级别.虽然我们可以在测试通过时忽略日志,但是如果需要诊断失败的测试,选择正确的日志级别是非常重要的. 2.日志级别的重要性 ...
- Spring Boot项目中如何定制拦截器
本文首发于个人网站:Spring Boot项目中如何定制拦截器 Servlet 过滤器属于Servlet API,和Spring关系不大.除了使用过滤器包装web请求,Spring MVC还提供Han ...
- Spring Boot项目中如何定制PropertyEditors
本文首发于个人网站:Spring Boot项目中如何定制PropertyEditors 在Spring Boot: 定制HTTP消息转换器一文中我们学习了如何配置消息转换器用于HTTP请求和响应数据, ...
- Spring框架学习笔记(6)——阿里云服务器部署Spring Boot项目(jar包)
最近接外包,需要部署服务器,便是参考了网上的几篇博文,成功在阿里云服务器成功部署了Spring Boot项目,特记下本篇笔记 Spring Boot项目打包 这里说一下部署的一些问题 1.mysql驱 ...
随机推荐
- Java,用户刷屏检测\相似字符串检测
背景 近期有几个业务方提出一需求,期望判断一个用户在短期内是否存在刷屏现象,出现后能对其做出限制,并上报. 刷屏定义:取出用户近期20条评论,如果有50%的评论是"相似"的,则认为 ...
- python 定时任务APScheduler 使用介绍
python 定时任务APScheduler 使用介绍 介绍: APScheduler的全称是Advanced Python Scheduler.它是一个轻量级的 Python 定时任务调度框架. ...
- 为Go项目编写Makefile
为Go项目编写Makefile 借助Makefile我们在编译过程中不再需要每次手动输入编译的命令和编译的参数,可以极大简化项目编译过程. make介绍 make是一个构建自动化工具,会在当前目录下寻 ...
- GO语言常用标准库03---time包
package main import ( "fmt" "time" ) func main021() { nowTime := time.Now() fmt. ...
- Python+Selenium学习笔记7 - os模块
os模块是关于文件/目录方面的 导入语法 import os 相关方法 path.abspath() 用来获取当前路径下的文件 os.path.abspath('checkbox.html') ...
- Django(51)drf渲染模块源码分析
前言 渲染模块的原理和解析模块是一样,drf默认的渲染有2种方式,一种是json格式,另一种是模板方式. 渲染模块源码入口 入口:APIView类中dispatch方法中的:self.response ...
- 用NumPy genfromtxt导入数据
用NumPy genfromtxt导入数据 NumPy provides several functions to create arrays from tabular data. We focus ...
- Salesforce LWC学习(三十四) 如何更改标准组件的相关属性信息
本篇参考: https://www.cnblogs.com/zero-zyq/p/14548676.html https://www.lightningdesignsystem.com/platfor ...
- P1831 杠杆数(数位Dp)
题目描述 如果把一个数的某一位当成支点,且左边的数字到这个点的力矩和等于右边的数字到这个点的力矩和,那么这个数就可以被叫成杠杆数. 比如$4139$就是杠杆数,把3当成支点,我们有这样的等式:$4 \ ...
- P2782 友好城市(最长不下降子序列)
题目描述 有一条横贯东西的大河,河有笔直的南北两岸,岸上各有位置各不相同的$N$个城市.北岸的每个城市有且仅有一个友好城市在南岸,而且不同城市的友好城市不相同.每对友好城市都向政府申请在河上开辟一条直 ...