嵌入式Redis服務(wù)器在Spring Boot測(cè)試中的使用教程
1、概述
Spring Data Redis
提供了一種與Redis實(shí)例集成的簡(jiǎn)單方法。
但是,在某些情況下,使用嵌入式服務(wù)器比使用真實(shí)服務(wù)器創(chuàng)建開發(fā)和測(cè)試環(huán)境更方便。
因此,我們將學(xué)習(xí)如何設(shè)置和使用嵌入式Redis服務(wù)器。
2、依賴
讓我們首先添加必要的依賴項(xiàng):
<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>
這個(gè)spring-boot-starter-test
包含我們需要運(yùn)行集成測(cè)試的各種依賴。
此外,embedded-redis
包含我們將使用的嵌入式服務(wù)器。
3、設(shè)置
添加依賴項(xiàng)后,我們應(yīng)該定義Redis服務(wù)器和我們的應(yīng)用程序之間的連接設(shè)置。
讓我們首先創(chuàng)建一個(gè)類來(lái)保存我們的屬性:
@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 }
接下來(lái),我們應(yīng)該創(chuàng)建一個(gè)配置類來(lái)定義連接并使用我們的屬性:
@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; } }
配置非常簡(jiǎn)單。這樣我們的嵌入式服務(wù)器可以在其他的端口上運(yùn)行。
4、嵌入式Redis服務(wù)器
現(xiàn)在,我們將配置嵌入式服務(wù)器并在我們的一項(xiàng)測(cè)試中使用它。
首先,讓我們?cè)跍y(cè)試的資源目錄(src/test/resources)中創(chuàng)建一個(gè)application.properties文件:
spring.redis.host=localhost spring.redis.port=6370
之后,我們將創(chuàng)建一個(gè)@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(); } }
當(dāng)context上下文啟動(dòng),服務(wù)器就跟著啟動(dòng)。它根據(jù)我們?cè)趯傩灾卸x的端口運(yùn)行在我們的機(jī)器上。有了它,我們現(xiàn)在可以在不停止實(shí)際Redis服務(wù)器的情況下運(yùn)行測(cè)試了。
理想情況下,我們希望在隨機(jī)可用端口上啟動(dòng)它,但嵌入式Redis尚不具備此功能。我們現(xiàn)在可以做的是通過(guò)ServerSocket API 獲取隨機(jī)端口。
此外,當(dāng)上下文停止,服務(wù)器也跟著停止。
服務(wù)器也可以由我們自己的可執(zhí)行文件來(lái)提供:
this.redisServer = new RedisServer("/path/redis", redisProperties.getRedisPort());
此外,可執(zhí)行文件可以按不同的操作系統(tǒng)來(lái)定義:
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());
最后,讓我們創(chuàng)建一個(gè)使用TestRedisConfiguration類的測(cè)試吧:
@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服務(wù)器。
此外,我們必須手動(dòng)將TestRedisConfiguration
添加到SpringBootTest
。正如我們之前所說(shuō),服務(wù)器在測(cè)試之前啟動(dòng)并在測(cè)試之后停止。
5、結(jié)論
嵌入式Redis服務(wù)器是在測(cè)試環(huán)境中替換實(shí)際服務(wù)器的完美工具。我們已經(jīng)看到了如何配置它以及如何在我們的測(cè)試中使用它。
到此這篇關(guān)于嵌入式Redis服務(wù)器在Spring Boot測(cè)試中的使用的文章就介紹到這了,更多相關(guān)Redis Spring Boot使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
redis.conf中使用requirepass不生效的原因及解決方法
本文主要介紹了如何啟用requirepass,以及啟用requirepass為什么不會(huì)生效,從代碼層面分析了不生效的原因,以及解決方法,需要的朋友可以參考下2023-07-07RedisDesktopManager?連接redis的方法
這篇文章主要介紹了RedisDesktopManager?連接redis,需要的朋友可以參考下2023-08-08Redis和Nginx實(shí)現(xiàn)限制接口請(qǐng)求頻率的示例
限流就是限制API訪問(wèn)頻率,當(dāng)訪問(wèn)頻率超過(guò)某個(gè)閾值時(shí)進(jìn)行拒絕訪問(wèn)等操作,本文主要介紹了Redis和Nginx實(shí)現(xiàn)限制接口請(qǐng)求頻率的示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02