I have some cache-class witch aggregates some heavy objects for optimization in production. I need to test code witch using cache but logic not related to cache. How can I avoid using cache? And run getting objects on-the-fly, not using cache. I thought about reflection, but it force me to suffer. Is it design problem or maybe I try to do something wrong?
case class SomeObject()
class FooService(private val cacheMap: collection.mutable.Map[String, SomeObject]) {
/// some cache logic with put objects to cache
def put(key: String, value: SomeObject): collection.mutable.Map[String, SomeObject] = cacheMap += key -> value
def get(key: String): Option[SomeObject] = cacheMap.get(key)
private def loadFromFile(file: String): SomeObject = ???
def loadObject(key: String): SomeObject = {
// ... some logic from loading
cacheMap.getOrElseUpdate(key, loadFromFile("some_path"))
}
}
class Bar {
val fooCache = new FooService(collection.mutable.Map.empty[String, SomeObject])
def methodForTest(args: List[String]): SomeObject = {
// do some logic ...
val someObject = fooCache.loadObject("someKey") // here I can't create filePath for loading directly from file
// fooCache updates, but I don't want it
someObject
}
}
Aucun commentaire:
Enregistrer un commentaire