feat(cache): 自动装配 + spring.factories 注册
This commit is contained in:
54
src/test/java/com/njcn/middle/cache/autoconfig/RedisCacheAutoConfigurationTest.java
vendored
Normal file
54
src/test/java/com/njcn/middle/cache/autoconfig/RedisCacheAutoConfigurationTest.java
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
package com.njcn.middle.cache.autoconfig;
|
||||
|
||||
import com.njcn.middle.cache.support.CacheKeyBuilder;
|
||||
import com.njcn.middle.cache.support.CacheValueCodec;
|
||||
import com.njcn.middle.cache.template.RedisCacheEnhanceTemplate;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
class RedisCacheAutoConfigurationTest {
|
||||
|
||||
private final ApplicationContextRunner runner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(
|
||||
RedisAutoConfiguration.class, RedisCacheAutoConfiguration.class))
|
||||
.withPropertyValues("spring.redis.host=127.0.0.1", "spring.redis.port=6379");
|
||||
|
||||
@Test
|
||||
void registersCacheBeans() {
|
||||
runner.run(ctx -> {
|
||||
assertThat(ctx).hasSingleBean(RedisCacheEnhanceTemplate.class);
|
||||
assertThat(ctx).hasSingleBean(CacheKeyBuilder.class);
|
||||
assertThat(ctx).hasSingleBean(CacheValueCodec.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void backsOffWhenUserDefinesOwnTemplate() {
|
||||
runner.withUserConfiguration(CustomConfig.class).run(ctx -> {
|
||||
assertThat(ctx).hasSingleBean(RedisCacheEnhanceTemplate.class);
|
||||
assertThat(ctx.getBean(RedisCacheEnhanceTemplate.class))
|
||||
.isSameAs(ctx.getBean("myTemplate"));
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class CustomConfig {
|
||||
@Bean
|
||||
RedisCacheEnhanceTemplate myTemplate() {
|
||||
return new RedisCacheEnhanceTemplate(
|
||||
new StringRedisTemplate(mock(RedisConnectionFactory.class)),
|
||||
new CacheKeyBuilder("cache:", false, ""),
|
||||
new CacheValueCodec(),
|
||||
new RedisCacheProperties());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user