【发布时间】:2017-05-12 02:01:02
【问题描述】:
我创建了如下简单的缓存应用程序:
import org.ehcache.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.cache.CacheException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class JsonObjectCacheManager {
private static final Logger logger = LoggerFactory.getLogger(JsonObjectCacheManager.class);
private final Cache<String, JsonObjectWrapper> objectCache;
//setting up cache
public JsonObjectCacheManager() {
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("jsonCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, JsonObjectWrapper.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(100, EntryUnit.ENTRIES)
.offheap(10, MemoryUnit.MB))
.withExpiry(Expirations.timeToLiveExpiration(Duration.of(5, TimeUnit.MINUTES)))
.withValueSerializingCopier()
.build())
.build(true);
objectCache = cacheManager.getCache("jsonCache", String.class, JsonObjectWrapper.class);
}
public void putInCache(String key, Object value) {
try {
JsonObjectWrapper objectWrapper = new JsonObjectWrapper(value);
objectCache.put(key, objectWrapper);
} catch (CacheException e) {
logger.error(String.format("Problem occurred while putting data into cache: %s", e.getMessage()));
}
}
public Object retrieveFromCache(String key) {
try {
JsonObjectWrapper objectWrapper = objectCache.get(key);
if (objectWrapper != null)
return objectWrapper.getJsonObject();
} catch (CacheException ce) {
logger.error(String.format("Problem occurred while trying to retrieveSpecific from cache: %s", ce.getMessage()));
}
logger.error(String.format("No data found in cache."));
return null;
}
public boolean isKeyPresent(String key){
return objectCache.containsKey(key);
}
}
JsonObjectWrapper 只是一个包装类,用于包装 Object 以便可以对其进行序列化。
@Getter
@Setter
@ToString
@AllArgsConstructor
public class JsonObjectWrapper implements Serializable {
private static final long serialVersionUID = 3588889322413409158L;
private Object jsonObject;
}
我已经编写了如下的 JUnit 测试:
import org.junit.*;
import org.mockito.*;
import java.util.*;
import static org.junit.Assert.*;
@RunWith(MockitoJUnitRunner.class)
public class JsonObjectCacheManagerTest {
private JsonObjectCacheManager cacheManager;
private Map<String, Object> names;
@Before
public void setup(){
/*names = new HashMap(){
{
put("name1", "Spirit Air Lines");
put("name2", "American Airlines");
put("name3", "United Airlines");
}
};*/
//edited as per Henri's point and worked
names = new HashMap();
names.put("name1", "Spirit Air Lines");
names.put("name2", "American Airlines");
names.put("name3", "United Airlines");
cacheManager = new JsonObjectCacheManager();
}
@Test
public void isPresentReturnsTrueIfObjectsPutInCache() throws Exception {
//put in cache
cacheManager.putInCache("names",names);
assertTrue(cacheManager.isKeyPresent("names"));
}
@Test
public void cacheTest() throws Exception {
//put in cache
cacheManager.putInCache("names",names);
//retrieve from cache
Map<String, Object> namesFromCache = (Map<String, Object>) cacheManager.retrieveFromCache("names");
//validate against the cached object
// assertEquals(3, namesFromCache.size());
// assertEquals("American Airlines", namesFromCache.get("name2"));
}
}
我收到断言错误,说缓存中不存在密钥。这意味着对象没有被添加到缓存中。
有没有办法为此做junit测试?感谢您的帮助。
编辑: 嘿伙计们,@Henri 指出了我的错误,它解决了我的问题。 :)
【问题讨论】:
-
你的
JsonObjectWrapper类是什么,它的getJsonObject方法有什么作用?它的名字表明它可能不是您想要在retrieveFromCache中调用的方法 - 如果您希望通过此测试,您希望那里有原始(非 JSON)对象。 -
@DavidWallace 我想防止缓存对象的突变,所以被缓存的对象应该实现 Serializable 接口,文档在ehcache.org/documentation/3.0/serializers-copiers.html。 getJsonObject() 只是 getter 方法——>请看我正在使用 Lombok 插件来生成 getter/setter/cons。调用它只是为了解开缓存的 json 对象。
标签: java caching mockito junit4 ehcache