java - 如何在方法上测试 Spring 的 Cacheable?
问题描述
最近在我的项目中,我被要求将 Spring 的 @Cacheable 注释用于从数据库返回静态 referenceData 的方法之一。我关注了这个博客https://medium.com/@d.lopez.j/configuring-multiple-ttl-caches-in-spring-boot-dinamically-75f4aa6809f3,它指导动态创建缓存。我正在尝试通过遵循这个 SO 答案来测试这个实现如何在 Spring Data 存储库上测试 Spring 的声明性缓存支持?我正面临问题。我无法将 application-test.properties 中的属性加载到我的测试类中。
我的 CachingConfig 类
package org.vinodh.testing;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.benmanes.caffeine.cache.Caffeine;
import lombok.Data;
@Configuration
@ConfigurationProperties(prefix = "caching")
@Data
public class CachingConfig {
private Map<String, CacheSpec> specs;
@Data
public static class CacheSpec {
private int minutesToExpire;
private int maximumSize;
public int getMaximumSize() {
return maximumSize;
}
public void setMaximumSize(int maximumSize) {
this.maximumSize = maximumSize;
}
public int getMinutesToExpire() {
return minutesToExpire;
}
public void setMinutesToExpire(int minutesToExpire) {
this.minutesToExpire = minutesToExpire;
}
}
public Map<String, CacheSpec> getSpecs() {
return specs;
}
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
if (specs != null) {
List<CaffeineCache> caches = specs.entrySet().stream()
.map(entry -> buildCache(entry.getKey(), entry.getValue())).collect(Collectors.toList());
cacheManager.setCaches(caches);
}
return cacheManager;
}
private CaffeineCache buildCache(String name, CacheSpec specs) {
Caffeine<Object, Object> caffeineBuilder = Caffeine.newBuilder()
.expireAfterWrite(specs.getMinutesToExpire(), TimeUnit.MINUTES).maximumSize(specs.getMaximumSize());
return new CaffeineCache(name, caffeineBuilder.build());
}
}
我的测试班
package org.vinodh.testing;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@ConfigurationProperties(prefix = "caching")
@ActiveProfiles("test")
@Profile("test")
public class CachingConfigTest {
@Configuration
@EnableCaching
static class NestedCacheConfiguration {
static class CacheSpec {
@SuppressWarnings("unused")
private int minutesToExpire;
@SuppressWarnings("unused")
private int maximumSize;
}
private Map<String, CacheSpec> specs;
public Map<String, CacheSpec> getSpecs() {
return specs;
}
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
System.out.println(getSpecs()); // Is null
return cacheManager;
}
}
@Test
public void test() {
System.out.println("Inside Test");
}
}
应用程序-test.properties
caching.specs.test.minutesToExpire=10
caching.specs.test.maximumSize=10
解决方案
推荐阅读
- r - R:同时使用 lapply 和 sink:创建文件并将输出存储在列表中?
- angular - 是否可以在组件上应用角度指令?
- ruby-on-rails - Capybara::ElementNotFound:找不到未禁用的“登录”按钮
- c# - 将 ComboBox 选择事件转换为复选框切换
- python - Keras - 关于输入层节点数量的混淆
- javascript - 谁能解释这段代码是如何工作的?
- pyomo - 访问 pyomo 变量的任何方式
- r - 在两个表中使用组 ID 滚动连接
- python-2.7 - 如何从父任务中清除所有 celery 子任务?
- python - 使用 MultiLabelBinarizer 转换 Keras 预测输出