我有一个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)));
}
}对于如何在这个过滤器中使用这个类的单元测试方法,有什么想法吗?提前谢谢,
发布于 2018-11-08 19:42:53
将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业务逻辑,而不必处理缓存的复杂性。
之后,您可能需要对缓存配置进行单独的测试,例如使用属性来定义过期超时。
https://stackoverflow.com/questions/53214709
复制相似问题