Fake HTTP request requests for testing on Android

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.

+4
source share
4 answers

Yes, you can definitely fake answers when using the HttpClient infrastructure. This is rather confusing, and I will have to leave most of the details to you, but I will give you a brief overview:

  • ClientHttpRequestFactory mainly so that you can override the createRequest() method so that you can ...

  • Return your own implementation of ClientHttpRequest , in which you can override the execute() method so that you can ...

  • Bring back your own ClientHttpResponse implementation in which you can finally return your fake response data, for example. getBody() can return the contents of a file, you can hard-code headers in getHeaders() , etc.

The rest will figure out how best to connect all these frauds with your level of service.

+2
source

You could give Charles a try something like this. Sort non-code solution.

http://www.charlesproxy.com/

I use Charles reverse proxies and a local tool for such things.

What you do indicates your request in your local field on the reverse proxy port. Charles, in turn, can be configured to provide a static hard-coded flat file, but for your application, it looks like a 100% genuine web service response.

There are many other interesting things you can do with Charles: track traffic from your Android application to and from your server and checkpoints (which allows you to configure requests and responses before sending and receiving them). Definitely worth checking out.

+2
source

Another option is to use Injection Dependency so that you can modify the HttpClient when running the tests. If you're interested, check out Guice .

0
source

I assume that you are interested in writing functional tests using the standard Android Junit platform. That way, you can simply implement the parts of the API that you use on your own web server and point to that server when you run your tests.

If you prefer your tests to be standalone, you could implement an Http server that runs on the device. Http server usage examples available in the Android class library, here and here .

0
source

Source: https://habr.com/ru/post/889749/


All Articles