DeveloperKurt
DeveloperKurt

Reputation: 788

Android UI Testing With MVVM

I use MVVM architecture and Dagger 2 in my application. I have a repository that gets data from servers, and when the data is retrieved ViewModel notifies the LiveData observer, which invokes the updateUI() method from my Activity.

So, when I am testing which views are visible with espresso, I want to wait until the updateUI() gets called. I cannot mock the ViewModel easily since I am using Dagger 2 but aside from that since it is an integration test, I don't want to do it in the first place.

Thread.sleep() would be an awful and naive solution for this, since too little waiting time would cause the test to fail, too much of it would cause to the loss of valuable time and it also depends on the testing and network environment!

So what do we do here, what's the optimal solution to this problem?

Upvotes: 2

Views: 1136

Answers (1)

DeveloperKurt
DeveloperKurt

Reputation: 788

With more research, I have found out that Espresso has already a solution to this problem called idling resources.

You can choose a suitable idling resource implementation from the list below to integrate it into your app.

You can start using it in your app by adding it to your Gradle file like this:

def espresso_version = '3.2.0' 

androidTestImplementation "androidx.test.espresso:espresso-core:$espresso_version"
implementation "androidx.test.espresso:espresso-idling-resource:$espresso_version"

Note that the updated Espresso version can found at Google's Maven Repository

Sample from Google

More details on idling resources

Upvotes: 1

Related Questions