2021-02-28 23:38:58 +08:00
|
|
|
|
package cn.iocoder.dashboard.config;
|
|
|
|
|
|
|
|
|
|
import com.github.fppt.jedismock.RedisServer;
|
|
|
|
|
import org.redisson.spring.starter.RedissonAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
|
|
|
|
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
|
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
2021-03-01 01:02:25 +08:00
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
2021-02-28 23:38:58 +08:00
|
|
|
|
@Lazy(false) // 禁用懒加载,因为需要保证 Redis Server 必须先启动
|
|
|
|
|
@EnableConfigurationProperties(RedisProperties.class)
|
|
|
|
|
@AutoConfigureBefore({RedisAutoConfiguration.class, RedissonAutoConfiguration.class}) // 在 Redis 自动配置前,进行初始化
|
|
|
|
|
public class RedisTestConfiguration {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建模拟的 Redis Server 服务器
|
|
|
|
|
*/
|
2021-03-01 00:21:39 +08:00
|
|
|
|
@Bean(destroyMethod = "stop")
|
2021-02-28 23:38:58 +08:00
|
|
|
|
public RedisServer redisServer(RedisProperties properties) throws IOException {
|
2021-03-01 00:21:39 +08:00
|
|
|
|
RedisServer redisServer = new RedisServer(properties.getPort());
|
2021-03-01 01:02:25 +08:00
|
|
|
|
// TODO 芋艿:一次执行多个单元测试时,貌似创建多个 spring 容器,导致不进行 stop。这样,就导致端口被占用,无法启动。。。
|
2021-03-01 00:21:39 +08:00
|
|
|
|
try {
|
|
|
|
|
redisServer.start();
|
|
|
|
|
} catch (Exception ignore) {}
|
|
|
|
|
return redisServer;
|
2021-02-28 23:38:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|