I am new to Kotlin and I am writing unit test for a method that tries to load a value from a map defined at class level (kind of like a local cache) and returns if the value is found otherwise a custom exception. I am having a hard time imagining how to do that.
I am using org.mockito.kotlin.whenever
to mock responses by other methods but not sure what i need to do in this case ?
Would appreciate the help and sorry if its a very basic question
I can’t think of a solution so would appreciate any pointers.
The code roughly looks like below. I am trying to write the tests for fetch()
method and load()
class Original {
private val cache = mutableMapOf<String,ClassAbc>()
// loads the value from cache and if not present then calls api and saves to cache
fun load(){
fetch()?. run{
cache["abc"]=callApi()
}
}
fun fetch(key: String) : Map<String, ClassAbc> {
// fetches the values from cache and returns an empty map if not found
finalMap = mutableMapOf<String, ClassAbc>()
cache[key]?.let {
finalMap[key] = it
}
return finalMap
}
Waqas Haider is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1