Im trying to come up with the best way to test a cache class that im currently using.... i would like to substitute ClientFactory below when this class is run in tests.... I like to leave the structure of the class as much as possible but since it has a private constructor im struggling to think of the best way to test it..
public class MyCache {
private final long TIME_OUT
private static MyCache instance = null;
private final HashMap<String, MyObject> cache = new HashMap<String, MyObject>();
private MyCache() {
}
public static MyCache getInstance() {
if (instance == null) {
instance = new MyCache();
}
return instance;
}
public MyObject getDetails(String id) throws Exception {
MyObject myObject = cache.get(id);
if (myObject != null) {
return myObject;
} else {
try {
// want to be able to replace ClientFactory with test stub
Client client = ClientFactory.createClient();
myObject = client.getMyObject(id);
} catch (NotFoundException nf) {
.... log error
}
return myObject;
}
}
}