java - 如何使用 cache2k 对过滤器进行单元测试
问题描述
我有一个使用缓存的 servlet 过滤器,代码基本上是这样的
public class CustomFilter implements Filter {
private final Cache<String, ClientRequest> cache;
@Autowired
public CustomFilter(Service service){
cache = Caching.getCachingProvider().getCacheManager()
.createCache("requestsCache",
ExtendedMutableConfiguration.of(
Cache2kBuilder.of(String.class,
ClientRequest.class).entryCapacity(100)
.expireAfterWrite(1000, TimeUnit.SECONDS)));
}
}
关于如何在使用此类的此过滤器中对方法进行单元测试的任何想法?提前致谢,
解决方案
将创建提取Cache<String, ClientRequest>
到外部配置并通过过滤器构造函数注入:
public class CustomFilter implements Filter {
private final Cache<String, ClientRequest> cache;
public CustomFilter(Cache<String, ClientRequest> cache) {
this.cache = Objects.requireNonNull(cache);
}
这样您就可以在单元测试中模拟缓存。这将允许单独测试CustomFilter
业务逻辑,而不必处理缓存的复杂性。
之后,您可能需要对缓存配置进行单独测试,例如通过使用属性来定义到期超时。
推荐阅读
- c# - 如何在 WPF 中激活 UserControl 的所有父级(即一个选项卡)
- sql - 使用 sql 查询从数据库中计算百分比(SQL Management Studio 2012)?
- ruby-on-rails - 使用 Turbolinks 5 和 Rails 5 在页面之间重新加载资产
- keras - 如何在 LSTM 自动编码器中计算带有零填充的输入的 softmax
- sql - 如何查询属性的未配对值
- amazon-s3 - 如何将文件从 AWS EFS 复制到 AWS S3?
- google-translation-api - 用于检查我在 Google Translation API 中的余额的 API
- python - 如何将 Pandas 系列转换为日期对象
- neural-network - 使用 TensorRT Caffe Parser 解析 Mobilenet-SSD 时出错,“ditcaffe.LayerParameter”没有名为“prior_box_param”的字段
- csv - Neo4j:导入 csv 最有效的解决方案是什么?