Hi!
I'm trying to do instrumented test on an app that is based on the architecture components. The testing environment is the same as in the github browser sample (no dagger2 injections, SingleFragmentActivity).
My dao returns a DataSource.Factory
It was easy without the Paging library, but now it seems a bit more complicated.
How should be the PagedList mocked, without creating an in memory databse to get the DataSource.Factory, then creating a LivePagedListBuilder to get PagedList object which contains the test data (which need to be inserted first to get some result)?
Thanks
I have a similar question: https://stackoverflow.com/questions/50435770/how-do-i-create-a-pagedlist-of-an-object-for-tests
You need to subclass the DataSource.Factory, and in the create() method just return a new LimitOffsetDataSource instance.
In LimitOffsetDataSource you must override convertRows(), countItems() and loadRange() methods. After you have a datasource instance, you can pass it to a LivePagedListBuilder.
@kekefigure I pretty much followed your instruction and my tests were working fine until I added IntentsTestRule
Now it throws "java.lang.IllegalStateException: Cannot access database on the main thread"
Have you got it to work with IntentsTestRule?
Here is my implementation BTW https://github.com/saied89/FilmCompass/blob/master/app/src/androidTest/java/android/saied/com/filmcompass/PagedListMock.kt
@saied89 You mean InstantTaskExecutorRule, right? Because it replaces the background executor with the mainthread executor, so the exception is correct.
Did you allow main thread queries on the room db instance with allowMainThreadQueries() ?
BTW a full stack trace would be useful
No I meant IntentsTestRule. The problem was that I was mixing ActivityScenario api with the old ActivityTestRule api which the IntentsTestRule extends.
p.s: I'm so sorry for delayed response.
@saied89 Your link above is no longer active. Do you still have a working solution available to share for mocking the PagedList?
Thanks @saied89! This is great. I'm working on putting a talk together on Android testing with Kotlin, JUnit 5, and MockK. The end result will be testing the ViewModel in a local unit test that mocks the Repository.
I'm working on the proof of concept this week and will report back once it's working. I'm happy to provide you credit for this important piece! 🙂
That would be nice :). I'm pretty sure it's working. Not sure if there's
been an official solution in the mean time though. Good luck with the talk!
On Wed, Sep 4, 2019, 5:48 AM AdamHurwitz notifications@github.com wrote:
Thanks @saied89 https://github.com/saied89! This is great. I'm working
on putting a talk together on Android testing with Kotlin, JUnit 5, and
MockK. I'm working on the proof of concept this week and will report back
once it's working. I'm happy to provide you credit for this important
piece! :-)—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/googlesamples/android-architecture-components/issues/287?email_source=notifications&email_token=AEZS2NO6XFLVM3YJIUB7IU3QH4EGNA5CNFSM4EL7VJUKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD52A3IA#issuecomment-527699360,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AEZS2NIVLBLIXCFLKEVYAULQH4EGNANCNFSM4EL7VJUA
.
Thank you @saied89!
I'll follow-up with Google's @JoseAlcerreca who works on LiveData, after testing your solution. There ought to be an official solution as PagedList's are key to Android's architecture components.
Also, you should post your solution to this related StackOverflow post. I'm happy to do so if you don't have the bandwidth and can link to your GitHub profile.
Update @saied89 - Your implementation of the PagedList is working in my sample test outlined in this StackOverflow answer. The full sample code may be found in the Coinverse Open App.
In addition to the solutions mentioned above, Paging3 now offers PagingData.from(list) for static lists and PagingData.empty() for empty lists.
Thanks for the update Dustin! I'm happy to hear that local JUnit 5 tests will be easier moving forward with Paging3.
Most helpful comment
You need to subclass the DataSource.Factory, and in the
create()method just return a new LimitOffsetDataSource instance.In LimitOffsetDataSource you must override
convertRows(),countItems()andloadRange()methods. After you have a datasource instance, you can pass it to a LivePagedListBuilder.