Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrumented tests with the paging library #287

Closed
kekefigure opened this issue Jan 16, 2018 · 13 comments
Closed

Instrumented tests with the paging library #287

kekefigure opened this issue Jan 16, 2018 · 13 comments
Labels

Comments

@kekefigure
Copy link

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<Integer, ModelSomething> and with the LivePagedListBuilder i get a LiveData<PagedList> object in the repository. I'm observing this livedata in the fragment, and replace the adapters list on data changes.

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

@isuPatches
Copy link

I have a similar question: https://stackoverflow.com/questions/50435770/how-do-i-create-a-pagedlist-of-an-object-for-tests

@kekefigure
Copy link
Author

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.

@saied89
Copy link

saied89 commented Jan 22, 2019

@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

@kekefigure
Copy link
Author

@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

@saied89
Copy link

saied89 commented Jan 25, 2019

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.

@adam-hurwitz
Copy link

adam-hurwitz commented Sep 4, 2019

@saied89 Your link above is no longer active. Do you still have a working solution available to share for mocking the PagedList?

@saied89
Copy link

saied89 commented Sep 4, 2019

@AdamSHurwitz Here you go
https://github.com/saied89/DVDPrism/blob/master/app/src/androidTest/java/com/saied/dvdprism/app/PagedListMock.kt

@adam-hurwitz
Copy link

adam-hurwitz commented Sep 4, 2019

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! 🙂

@saied89
Copy link

saied89 commented Sep 4, 2019 via email

@adam-hurwitz
Copy link

adam-hurwitz commented Sep 4, 2019

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.

@adam-hurwitz
Copy link

adam-hurwitz commented Sep 8, 2019

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.

recker115 pushed a commit to recker115/architecture-components-samples that referenced this issue Nov 12, 2019
recker115 pushed a commit to recker115/architecture-components-samples that referenced this issue Nov 12, 2019
)

* Update todo-mvp README.md

Refactored the content around a new structure, with new standard titles used for each sample
Fixed various grammar issues
Applied formatting
Additional explanations and links where appropriate
Introduced pre-requisite topics

* Update README.md

Removed a redundant sentence.

* copies Firebase Test Lab results to CIRCLE_TEST_REPORTS

* Replaced deprecated attribute

* Bumps versions and fixes annotation imports

* Fixes toolbar title not being persisted after rotation

* Fixes navigation issue android#287

* Bumps circle CI Android dependencies

* Fixes comment in test

* Bumps versions in travis.yml

* Moving the setting of the AddEditTaskPresenter in the constructor.
Tests added to check that the presenter is set to the view.

* Removing the `_id` column and making `entryid` primary key.
Moved the `TasksLocalDataSourceTest` to correct package.

* Fixed Firebase Test Lab script

* Tries to fix circle adding google repo

* Buildtools 26 is mandatory now, apparently

* More CI fixes

* CI caught some merge errors + circle fix

* Bumps versions to RC1, etc
recker115 pushed a commit to recker115/architecture-components-samples that referenced this issue Nov 12, 2019
* Update todo-mvp README.md

Refactored the content around a new structure, with new standard titles used for each sample
Fixed various grammar issues
Applied formatting
Additional explanations and links where appropriate
Introduced pre-requisite topics

* Update README.md

Removed a redundant sentence.

* copies Firebase Test Lab results to CIRCLE_TEST_REPORTS

* Replaced deprecated attribute

* Bumps versions and fixes annotation imports

* Fixes toolbar title not being persisted after rotation

* Fixes navigation issue android#287

* Bumps circle CI Android dependencies

* Fixes comment in test

* Bumps versions in travis.yml

* Moving the setting of the AddEditTaskPresenter in the constructor.
Tests added to check that the presenter is set to the view.

* Removing the `_id` column and making `entryid` primary key.
Moved the `TasksLocalDataSourceTest` to correct package.

* First commit: MPV with Room

* Readme updated

* Readme updated

* Fixing CI

* Fixing tests for local repository

* Using Idling resources for tests, for commands that are executed on the background thread

* comments updated

* Fixed Firebase Test Lab script

* fix typo in androidTest

* Fix typo (android#415)

- Change "compliment" to "complement"

* Fixed the typo (android#425)

* Tries to fix circle adding google repo

* Buildtools 26 is mandatory now, apparently

* More CI fixes

* CI caught some merge errors + circle fix

* Updates todo-mvvm-databinding with changes from todo-mvp (android#437)

* Update todo-mvp README.md

Refactored the content around a new structure, with new standard titles used for each sample
Fixed various grammar issues
Applied formatting
Additional explanations and links where appropriate
Introduced pre-requisite topics

* Update README.md

Removed a redundant sentence.

* copies Firebase Test Lab results to CIRCLE_TEST_REPORTS

* Replaced deprecated attribute

* Bumps versions and fixes annotation imports

* Fixes toolbar title not being persisted after rotation

* Fixes navigation issue android#287

* Bumps circle CI Android dependencies

* Fixes comment in test

* Bumps versions in travis.yml

* Moving the setting of the AddEditTaskPresenter in the constructor.
Tests added to check that the presenter is set to the view.

* Removing the `_id` column and making `entryid` primary key.
Moved the `TasksLocalDataSourceTest` to correct package.

* Fixed Firebase Test Lab script

* Tries to fix circle adding google repo

* Buildtools 26 is mandatory now, apparently

* More CI fixes

* CI caught some merge errors + circle fix

* Fixed Firebase Test Lab script (android#440)

* Updates room branch to API 26, Espresso, Arch Components

* Adds google m2 repository

* Adding google() to repositories

* Reverts to todo-mvp's README

* No need for LicecycleActivities now
@dlam
Copy link
Contributor

dlam commented Aug 8, 2020

In addition to the solutions mentioned above, Paging3 now offers PagingData.from(list) for static lists and PagingData.empty() for empty lists.

@dlam dlam closed this as completed Aug 8, 2020
@adam-hurwitz
Copy link

Thanks for the update Dustin! I'm happy to hear that local JUnit 5 tests will be easier moving forward with Paging3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants