I am writing an Android application that sometimes requires requesting data via HTTP from the REST API. I am using Apache DefaultHttpClient to execute requests. Is there a way to write tests for this application and "replace" the DefaultHttpClient response when running the tests so that the test results are always consistent?
As an example of what I would like to check, one of the web services that I am accessing takes a string and does a text search, returning an uploadable list of objects. I need to check cases where the list is empty, the list matches the first page, or the list is larger than the page, and the application needs to make a few queries to get the full list.
I am not a developer of this web API and cannot change its answers, so I cannot change what it returns. In the above example, if I want to check the case when the returned list is empty, I could just look for a string that, I am sure, will not return any results, but in the other two cases is more difficult, the return always changes.
I think that ideally I would have a way to get a modified DefaultHttpClient when running tests that returns hard code for requests to this URL instead of actually executing a network request. That way, I would always get consistent results regardless of the real web service response.
I am currently using Robotium for testing, but I am open to using other tools.
source share