RedisTestConfiguration.java 1.1 KB

123456789101112131415161718192021222324252627282930
  1. package cn.iocoder.dashboard.config;
  2. import com.github.fppt.jedismock.RedisServer;
  3. import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
  4. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.Lazy;
  8. import java.io.IOException;
  9. @Configuration(proxyBeanMethods = false)
  10. @Lazy(false) // 禁止延迟加载
  11. @EnableConfigurationProperties(RedisProperties.class)
  12. public class RedisTestConfiguration {
  13. /**
  14. * 创建模拟的 Redis Server 服务器
  15. */
  16. @Bean(destroyMethod = "stop")
  17. public RedisServer redisServer(RedisProperties properties) throws IOException {
  18. RedisServer redisServer = new RedisServer(properties.getPort());
  19. // TODO 芋艿:一次执行多个单元测试时,貌似创建多个 spring 容器,导致不进行 stop。这样,就导致端口被占用,无法启动。。。
  20. try {
  21. redisServer.start();
  22. } catch (Exception ignore) {}
  23. return redisServer;
  24. }
  25. }