I am trying to unit test*<https://stackoverflow.co...
# android
z
I am trying to unit test* a Room Dao Query that Returns a PagingSource From Paging 3*, i found the SO question that describes exactly what i was looking for but there is no answer, has anyone figured out how to test PagingSource? https://stackoverflow.com/questions/65164206/how-to-unit-test-a-room-dao-query-that-returns-a-pagingsource-from-paging-3
s
hey can you check if this answer helps you? @ziv kesten https://stackoverflow.com/a/56787814/1281930
🙏 1
b
I was using Paging 3 recenly (like 1 month ago) and as I remember there were no good testing APIs yet, seems like nothing changed yet
😭 1
z
@Sinan Gunes This fails with
cannot be cast to class androidx.room.paging.LimitOffsetDataSource
And that is kind of a hack with the paging 2 library, where as i am trying to test the paging 3 library. the PagingSource was introduced in paging 3. I appreciate you help anyway, tnx!
s
ok, it seems I haven’t updated my library for a while, let me see if I can find a way around.
👏 1
@ziv kesten what is your Room library version?
I am not observing this error. maybe it is the wrapping braces
{}
?
z
my version is 2.3.0-alpha04. The error with the
{}
is:
class com.zk.trackshows.data.local.dao.DiscoverLocalDataSourceTest$saveChocolateToDbSavesData$1$pagingSourceFactory$1 cannot be cast to class androidx.room.paging.LimitOffsetDataSource
the error without the
{}
is:
class java.util.ArrayList cannot be cast to class androidx.room.paging.LimitOffsetDataSource
So it seems as though the room dao method that emits the PagingSource cannot be case to
LimitOffsetDataSource
s
hmm, maybe ArrayList is better, have you checked content of the List?
z
I apologize for the mistake earlier, it was not
java.util.ArrayList
when casting the dao method that emits the PagingSource to LimitOffsetDataSource, the error is:
class androidx.paging.LegacyPagingSource cannot be cast to class androidx.room.paging.LimitOffsetDataSource
Which is even more interesting, i will further explore.
d
For testing
PagingSource
I would query it directly and compare the
LoadResult.Page
PagingData
is just a wrapper around a stream of library-internal events
You need to collect from it into an adapter or a differ or some other presenter API and then retrieve the data using
differ.snapshot
z
The issue is not testing the PagingSource itself, rather mocking the paging source to test the local data source, which, in the case of room, is exposing a PagingSource.
d
Ideally, what are you trying to assert? Wouldn't comparing LoadResult.Page.data to what you expect the query to return be sufficient to test that your query is written correctly?
Sorry I'm trying to help, but I'm not understanding why mocking
PagingSource
is necessary vs calling
.load
directly
z
Thank you for the help, it is much appreciated. I have a local data source that exposes PagingSource, the paging source originates from room's dao interface, when testing my local data source I want to test a "pagedDataStream" function in the local data source, it returns a PagingSource from the room dao, my idea was to mock/fake/stub the room dao and return pagingSource who's content I would compare in the assertion. Am I going about it wrong?
d
I feel like two things are getting mixed here: 1. If you just want to test pagedDataStream, why do you care what PagingSource will load? It seems like you would be fine with a fake impl of your dao interface and checking it gets called in the right places or by checking reference equality for the emitted PagingSource, etc. 2. If you're trying to test the queries you setup in the dao itself, then it would make sense to check the content loaded by PagingSource, but in this case I would just get an instance and call .load() directly checking for LoadResult.Page without your local datasource wrapper