伪造HTTP请求响应以便在Android中进行测试(Faking HTTP request responses for testing in Android)

我正在编写一个Android应用程序,有时需要通过HTTP从REST API请求数据。 我正在使用Apache DefaultHttpClient来执行请求。 有没有办法为这个应用程序编写测试并在运行测试时“替换”DefaultHttpClient的响应,以便测试结果始终一致?

作为我想要测试的一个例子,我正在访问的一个Web服务接受一个字符串并执行文本搜索,返回一个分页的对象列表。 我需要测试列表为空,列表适合第一页,或列表大于页面的情况,应用程序需要多次请求才能获得完整列表。

我不是这个Web API的开发者,也不能修改它的响应,所以我不能改变它返回的内容。 对于上面的例子,如果我想测试返回的列表是空的情况,我可以只搜索一个我肯定不会返回任何结果的字符串,但另外两个案例更难,因为服务可以回报总是在变化。

我认为理想情况下,我可以在运行测试时获得修改后的DefaultHttpClient,它会返回给定URL的请求的硬编码结果,而不是实际执行网络请求。 这样,我总能获得与真实Web服务响应无关的一致结果。

我目前正在使用Robotium进行测试,但我也愿意使用其他工具。

I'm writing an Android app which sometimes needs to request data through HTTP from a REST API. I'm using the Apache DefaultHttpClient for performing requests. Is there a way to write tests for this app and "replace" DefaultHttpClient's response when running the tests so that test results are always consistent?

As an example of the things I'd like to test, one of the web services I'm accessing takes a string and performs a text search, returning a paged list of objects. I need to test the cases where the list is empty, the list fits in the first page, or the list is larger than a page and the app needs to make several requests to get the complete list.

I'm not the developer of this web API nor can modify its responses, so I can't change what it returns. For the above example, if I want to test the case where the list returned is empty, I could just search for a string which I'm sure won't return any results, but the other two cases are harder because what the service can return is always changing.

I think ideally I would have a way to get a modified DefaultHttpClient when running tests, that returns a hardcoded result for requests to a given URL instead of actually doing the network request. This way I would always get consistent results independently of the real web service's response.

I'm currently using Robotium for testing but I'm open to using other tools too.

最满意答案

是的,在使用HttpClient框架时,您绝对可以“伪造”响应。 这是非常令人费解的,我将不得不将大部分细节留给您,但我会给您一个快速概述:

实现ClientHttpRequestFactory ,主要是因为你可以覆盖createRequest()方法,这样你就可以......

返回ClientHttpRequest的自定义实现,您可以在其中覆盖execute()方法,以便...

返回ClientHttpResponse的自定义实现,您最终可以返回虚假的响应数据,例如getBody()可以返回文件的内容,您可以在getHeaders()对标头进行硬编码等。

其余的是弄清楚如何最好地将所有这些恶作剧绑定到您的服务层。

Yes, you can definitely "fake" responses when using the HttpClient framework. It's quite convoluted, and I will have to leave most of the details up to you, but I will give you a quick overview:

Implement ClientHttpRequestFactory, mainly so you can override the createRequest() method so you can...

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

Return your custom implementation of ClientHttpResponse in which you will finally be able to return your fake response data, e.g. getBody() can return the content of a file, you can hardcode the headers in getHeaders(), etc.

The rest is figuring out how to best tie all these shenanigans to your service layer.

更多推荐