Issue:
I am working with Paging
library provided by Android Jetpack and I am not able to test the PagedList<T>
and DataSource.Factory<Int, T>
data used with PagedList.BoundaryCallback<T>()
in my ViewModel
My code for receiving PagedList
data using PagedList.BoundaryCallback<T>()
and PagedList.Config.Builder()
::
final var boundaryCallback = MyBoundaryCallback(
webServiceApi, mainRepository, PAGE_SIZE, appExecutors)
private final val pagedListConfig = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(PREFETCH_DISTANCE)
.setPageSize(PAGE_SIZE)
.build()
var pagedListData: LiveData<PagedList<MyData>> =
LivePagedListBuilder(dbDAO.loadAllPagedData(), pagedListConfig)
.setBoundaryCallback(boundaryCallback)
.build()
And my class MyBoundaryCallback :
class MyBoundaryCallback (private val webServiceApi: ApiService,
private val mainRepository: MainRepository,
private val networkPageLimit: Int,
private val appExecutors: AppExecutors) : PagedList.BoundaryCallback<MyData>() {
override fun onZeroItemsLoaded() {
webServiceApi.getData(0, networkPageLimit).enqueue(WebserviceCallback())
}
override fun onItemAtEndLoaded(itemAtEnd: MyData) {
webServiceApi.getData((itemAtEnd.id!! + 1), networkPageLimit).enqueue(WebserviceCallback())
}
private fun handleResponse(response: Response<List<MyData>>) {
appExecutors.diskIO().execute {
mainRepository.insertResultIntoDb(response.body())
}
}
inner class WebserviceCallback : Callback<List<MyData>> {
override fun onFailure(call: Call<List<MyData>>, t: Throwable) {
}
override fun onResponse(call: Call<List<MyData>>, response: Response<List<MyData>>) {
handleResponse(response)
}
}
}
Since the class MyBoundaryCallback
is pushing the data as you can see the code in my ViewModel
above, the LiveData
object is fetching the data from my room Db since it gives out DataSource.Factory<Int, T>
, find the code below :
@Query("SELECT * FROM mydata")
fun loadAllPagedData(): DataSource.Factory<Int, MyData>
I am not sure how to mock the PagedData
so if anyone has any idea how to approach this please share
For Simple List of data I am able to test it with something like the code below :
@Test
fun testBasicData() {
val dbMyData = MutableLiveData<List<MyData>>()
val observer = mock<Observer<List<MyData>>>()
Mockito.`when`(deliveriesDAO.loadAllData()).thenReturn(dbDeliveriesData)
val list = MyMockDataGenerator.createList(4)
dbDeliveriesData.observeForever(observer)
dbDeliveriesData.value = list
Mockito.verify(observer).onChanged(list)
}