伪造的HTTP请求响应为Android的测试 [英] Faking HTTP request responses for testing in Android
问题描述
我正在写一个Android应用程序有时需要请求通过HTTP数据从一个REST API。我使用了Apache DefaultHttpClient执行请求。有没有办法运行测试时,这样的测试结果总是一致的编写测试这个程序,并替换DefaultHttpClient的反应?
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?
作为的事情,我想测试中,Web服务,我访问一个例子将一个字符串进行文本搜索,返回对象的分页列表。我需要测试的情况下列表是空的,该列表装配在第一页,或者该列表大于页更大,该应用需要做出多个请求以获取完整列表。
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.
我不是这个网页API的开发者,也可以修改它的反应,所以我不能改变什么返回。对于上面的例子,如果我想测试在这里返回的列表是空的情况下,我可以只搜索,我敢肯定,将不会返回任何结果字符串,但其他两种情况都是因为什么服务可以更难回报总是在变化。
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.
我想最好我也有办法运行测试时,返回的请求到指定的URL,而不是实际上做的网络请求硬codeD结果得到修改DefaultHttpClient。这样,我总是会得到真正的Web服务的响应一致的结果独立。
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.
我目前使用Robotium测试,但我接受使用其他工具了。
I'm currently using Robotium for testing but I'm open to using other tools too.
推荐答案
是的,你绝对可以在使用HttpClient的框架假的反应。这是相当令人费解,我将不得不离开大多数细节给你,但我会给你一个简单概述:
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:
-
实施
ClientHtt prequestFactory
,主要是让你可以覆盖的createRequest()
方法,以便你可以...
Implement
ClientHttpRequestFactory
, mainly so you can override thecreateRequest()
method so you can...
返回您的自定义实施 ClientHtt prequest
,其中你可以覆盖执行()
方法,因此,你可以...
Return your custom implementation of ClientHttpRequest
, in which you can override the execute()
method so you can ...
返回您的自定义实施 ClientHtt presponse
中,你将最终能够回到你的假响应数据,例如: getBody()
可以返回一个文件的内容,可以硬code。在 getHeaders头()
等等。
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.
这篇关于伪造的HTTP请求响应为Android的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!