Android + Espresso + assync HTTP request -> how to test?

By : redrom
Source: Stackoverflow.com
Question!

i'm trying to test Android app using Espresso FW and I stucked on the problem with doing async HTTP request.

How can i test the case that app (activity) is waiting for processing request and after the response is received is displayed next activity with result or error message?

I tried to find any solution how to do it in the Espresso with testing against the data from the test server, but without luck.

Many thanks for any advice, example or link.

By : redrom


Answers
I struggled with this for a few days. If your app is using retrofit to handle HTTP requests, you can add this one line:

.setExecutors(AsyncTask.THREAD_POOL_EXECUTOR, new MainThreadExecutor())

to your RestAdaptor.Builder. This moves all of the HTTP requests into the AsyncTasks pool which is then handled by espresso.

I found the original answer here: http://www.michaelevans.org/blog/2015/08/03/using-espresso-for-easy-ui-testing/

By : Bebop_


I think the problem is in app architecture. You should use some Dependency Injection library to inject mock service with mock data in your application in test mode. When you inject "test" implementation ( mocks) when you testing. U will avoid problems with not network availability or you can simulate network availability by your test implementation.

By : mariopce


This video can help you solving your question :)
By: admin