如果您在运行测试用例时遇到问题(会有多个Applicaiton
s),您可以在创建via时使用 and 的虚拟实现SyncCacheApi
并AsyncCacheApi
覆盖绑定Application
provideApplication()
@Override
protected Application provideApplication() {
Application application = new GuiceApplicationBuilder().configure(testConfig)
.disable(EhCacheModule.class)
.overrides(bind(SyncCacheApi.class).to(FakeSyncCacheApi.class))
.bindings(new MyModule())
.in(new File(".")).build();
return application;
}
样本FakeSyncCacheApi
将类似于
@Singleton
public class FakeSyncCacheApi implements SyncCacheApi {
private LRUMap cache = new LRUMap();
@Override
public <T> T get(String key) {
return (T) cache.get(key);
}
@Override
public <T> T getOrElseUpdate(String key, Callable<T> block, int expiration) {
return getOrElseUpdate(key, block);
}
@Override
public <T> T getOrElseUpdate(String key, Callable<T> block) {
T value = (T) cache.get(key);
if (value == null) {
try {
value = block.call();
} catch (Exception e) {
}
cache.put(key, value);
}
return value;
}
@Override
public void set(String key, Object value, int expiration) {
cache.put(key, value);
}
@Override
public void set(String key, Object value) {
cache.put(key, value);
}
@Override
public void remove(String key) {
cache.remove(key);
}
}
这里的想法是禁用EhCache
模块并拥有我们自己的虚拟实现。