Android + Espresso + async HTTP request -> how to test? - android

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.

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/

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.

Related

How to perform a simple HTTP request in Kotlin/Android?

I am quite new to Android app development and would like to perform a simple API request via HTTP to a web server. URL("http://www.myapi.com").readText() seems to be a good way to start with, but this leads to a NetworkOnMainThreadException. So it must run asynchronously or in a coroutine. However, I'm not familiar enough with this and other examples I have found so far don't work for me.
Thanks a lot already for your help!
if you are using mvvm pattern you can do the following in your ViewModel class
viewModelScope.launch {val response = withContext(Dispatchers.IO) { your request }}
HTTP requests are no longer allowed by default on Android 9 and above.
Consider using HTTPS, or modify the AndroidManifest.xml, see this answer on StackOverflow.

How to test volley POST?

I want to test POST request on android. I don't have a web host. There are some android-volley request example in here.
How can I find a free url to allow post the requestes?
There a few i can think of :
https://httpbin.org/post
http://requestb.in/
www.posttestserver.co
I am sure there are more.
These are good for general experiments, however I would use something more customizable and locally installed like https://nodered.org/ and for actual tests something that actually runs on the device with the tests like:
MockWebserver
RESTMock
wiremock - example
Custom solution mocking the HttpStack(the last point before flushing the data to the wire)

No Connection error in Volley and Socket Time out in HttpClient by Async Task

I have spend near about 3 days in this issue of No Time error of Volley,
I am calling 10 web APIs sequentially, at 4th web API I am getting No Connection error of volley, out of 12 there are specifically 4 web APIs which are giving me same error(tries asynchronous as well).
I tried same by Async Task using Http Client getting Socket Time out error for the same web APis.
Note : All these web APIs are working in IOS as well as postman properly.
Can any one please let me know that this is the android side issue or from server side, if its of android please tell me the solution , how can I get rid of it?
I have resolved this issue after doing lots of work around.
Issue was of SSL Certification, As volley doesn't support SSL so need to allow externally.
To allow SSL Refer : here Create a HttpsTrustManager class that implements X509TrustManager:

Verify http requests made by android app while running espresso

While running UI Espesso tests I would like to also verify if the app is making correct http requests (e.g triggered by a button press). Currently using OkHttpClient client to make the requests.
Did you try something? I like to use Wiremock to mock http requests and check that the app have done the correct request. You must use the standalone version or you will get some issues with HttpClient.
For more information see http://wiremock.org/index.html

Android Loopj AndroidAsyncHttp mocking

is there are any way to mock Loopj AndroidAsyncHttp responses?
I am developing application which depends on REST API, but API is not ready yet and I want to develop application independently from API. Retrofit has a lot of mock implementations, but I was not able to find any solutions for AndroidAsyncHttp. Is there are any way?
I tried to send success message to handler on request creation handler.sendSuccessMessage(200, new Header[1], new byte[1]); , but onSuccess or something else is not called.
As example, you may create on any hosting php file with responsing json.
Or you can use my template for this cases http://so.five-dots.ru/api-test.php
Make responsing on this address and implements your logic. Then RESTfull API backend service will be ready, you can replace adresses on real and getting responseBody will reqirement data.

Categories

Resources