我创建了以下简单的缓存应用程序:
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只是包装对象的包装类,以便可以序列化。
@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指出了我的错误,这解决了我的问题。:)
问题是您的hashmap。实例化的方式创建了一个匿名的内部类。这是对外类实例的参考。这是测试类。而且测试类是不可序列化的。
如果您使用以下代码,所有测试都很好。
names = new HashMap();
names.put("name1", "Spirit Air Lines");
names.put("name2", "American Airlines");
names.put("name3", "United Airlines");
@Test public void cacheTest() throws Exception { cacheManager = Mockito.mock(JsonObjectCacheManager.class);
您必须不模拟测试的类。