使用 redis-mock 在自定义 Spring Boot Starter 中进行 Redis 测试

621 阅读2分钟

环境说明:

  • JDK 1.8
  • SpringBoot 2.7

在软件开发中,测试是至关重要的一环。然而,当涉及到一些依赖中间件服务器的情况时,测试就变得复杂起来。为了解决这个问题,我们可以利用 redis-mock 工具来模拟 Redis 服务器,从而在测试过程中不需要启动真实的 Redis 服务器。

这里面最重要的工具就是redis-mock 通过这个工具可以 mock 一个 RedisServer ,这样在测试的时候就不需要启动 redis 了。

环境准备

  • 依赖:
<!-- spring boot 配置所需依赖 -->  
<dependency>  
	<groupId>org.springframework.boot</groupId>  
	<artifactId>spring-boot-configuration-processor</artifactId>  
	<optional>true</optional>  
</dependency>  
  
<dependency>  
	<groupId>org.apache.commons</groupId>  
	<artifactId>commons-pool2</artifactId>  
</dependency>  
<dependency>  
	<groupId>org.springframework.boot</groupId>  
	<artifactId>spring-boot-starter-data-redis</artifactId>  
</dependency>  

<dependency>  
	<groupId>org.springframework.boot</groupId>  
	<artifactId>spring-boot-starter-test</artifactId>  
	<scope>test</scope>  
</dependency>  
 <!-- mock Redis -->  
<dependency>  
	<groupId>com.github.microwww</groupId>  
	<artifactId>mocker-redis-spring-boot-starter</artifactId>  
	<version>5.3.1</version>  
	<scope>test</scope>  
</dependency>
  • 配置文件
spring:  
  redis:  
    host: 127.0.0.1  
    port: 6380  
    password:  
    imeout: 10s  
    lettuce:  
      pool:  
        min-idle: 0  
        max-idle: 8  
        max-active: 8  
        max-wait: -1ms

Mock Redis Server

因为Mock服务需要在 SpringBoot 启动之前进行运行,所以通过实现 ApplicationRunner 接口来在 Spring Boot 启动前启动模拟的 Redis 服务器,以确保测试环境的准备工作顺利进行

public class MockRedisServerRunner implements ApplicationRunner {  
	RedisServer server = null;  
	@Override  
	public void run(ApplicationArguments args) throws IOException {  
	  
		server = new RedisServer();  
		String host = "127.0.0.1";  
		int port = 6380;  
		server.listener(host, port);  
		System.out.println("======== mock redis server open 6380 =========");  
	}  
	  
	@PreDestroy  
	public void onShutdown() throws IOException {  
		// 在应用程序关闭前执行的逻辑  
		server.close();  
		System.out.println("======== mock redis server close =========");  
	}  
  
}

编写 Test 代码

@SpringBootTest(classes = {MockRedisServerRunner.class , ShushanRedisAutoConfiguration.class , CacheProperties.class, RedisAutoConfiguration.class })  
public class RedisCacheApiTest {  
  
	@Autowired  
	CacheApi cacheApi ;  
  
	@Test  
	public void cacheGetAndSet(){  
		String key = "test";  
		String value = "666";  
		cacheApi.set(key,value);  
		Optional<Object> optional = cacheApi.get(key);  
		Assert.isTrue(optional.isPresent());  
		Assert.equals(value, optional.get());  
	}  
	  
	@Test  
	public void cacheGetAndSetObject(){  
		String key = "test";  
		List<Integer> value = ListUtil.of(1,2,3,4,5);  
		cacheApi.set(key,value);  
		Optional<List<Integer>> optional = cacheApi.get(key);  
		Assert.isTrue(optional.isPresent());  
		Assert.isTrue(CollectionUtil.isEqualList(value, optional.get()));  
	}  
  
}

这里的测试就是简单的 getset ,需要说明一下的是 @SpringBootTest(classes = {MockRedisServerRunner.class , ShushanRedisAutoConfiguration.class , RedisAutoConfiguration.class })

因为我是在自己写的 starter 中进行的测试,并没有 SpringBootApplication 也就是没有自动扫包 自动装配的存在,所以需要自己指定要进行 扫描加载的配置文件。 这里对配置中的几个class进行一个说明

  • MockRedisServerRunner :加载 Mock Reids Server 类
  • ShushanRedisAutoConfiguration : 我自己的配置文件,主要是注册我自己的Bean
  • RedisAutoConfiguration : SpringBoot 启动 Redis 需要用到的配置文件

通过以上方法,我们可以在自定义 Spring Boot Starter 中方便地进行 Redis 功能的测试,确保代码质量和功能稳定性。这种测试方式不仅能提高开发效率,同时也能保证系统功能的可靠性和稳定性。