Closed sarimmehdi closed 1 year ago
You can use Answer
to implement custom logic: https://stackoverflow.com/questions/44066993/using-mockito-doanswer-in-kotlin
I am adding my code here for anyone else running into the same problem:
repository = mock {
onBlocking { getAllComics() } doReturn flow {
emit(Resource.Loading())
emit(Resource.Success(comicsInDatabase))
}
onBlocking { getComics(any(), any()) } doAnswer {
flow {
val numsOfComicsToLoad = it.getArgument(0) as List<Int>
val loadFromDatabase = it.getArgument(1) as Boolean
emit(Resource.Loading())
val comics = if (loadFromDatabase) {
comicsInDatabase.filter { comic -> comic.num in numsOfComicsToLoad }
} else {
comicsInApi.filter { comic -> comic.num in numsOfComicsToLoad }
}
emit(Resource.Success(comics))
}
}
I am new to Mockito so I apologize if I don't get the general philosophy of this library. Documentation on this for the java version of mockito also doesn't really explain how to mock methods where the returned result depends on the parameters passed to the method. Below is the method I am trying to mock:
here's my attempt at trying to mock the above method (it is part of the repository class)
Any help regarding this would be appreciated