我正在尝试使用Robolectric测试框架的HTTP layer来测试HTTP调用。
我的测试类已经使用RunWith(RobolectricTestRunner.class)进行了注释。这是一个示例HTTP测试用例:
@Test
public void testHttpGet() {
Robolectric.addPendingHttpResponse(200, "All your requests are belong to Robolectric");
String response = ConnectionUtils.sendRequest();
assertThat(response, equalTo("All your requests are belong to Robolectric"));
}
此测试失败,JUnit Failure Trace报告以下DefaultRequestDirector异常(源自sendRequest()调用):
java.lang.NoSuchMethodError: org.apache.http.impl.client.DefaultRequestDirector.<init>(Lorg/apache/commons/logging/Log;Lorg/apache/http/protocol/HttpRequestExecutor;Lorg/apache/http/conn/ClientConnectionManager;Lorg/apache/http/ConnectionReuseStrategy;Lorg/apache/http/conn/ConnectionKeepAliveStrategy;Lorg/apache/http/conn/routing/HttpRoutePlanner;Lorg/apache/http/protocol/HttpProcessor;Lorg/apache/http/client/HttpRequestRetryHandler;Lorg/apache/http/client/RedirectHandler;Lorg/apache/http/client/AuthenticationHandler;Lorg/apache/http/client/AuthenticationHandler;Lorg/apache/http/client/UserTokenHandler;Lorg/apache/http/params/HttpParams;)V
at org.apache.http.impl.client.AbstractHttpClient.createClientRequestDirector(AbstractHttpClient.java:660)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:625)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
可能导致Apache类抛出异常的任何想法?
使用当前最新的库,robolectric-1.2-20120730.165026-113-jar-with-dependencies.jar。
答案 0 :(得分:5)
在Robolectric Google小组上找到答案:https://groups.google.com/forum/#!msg/robolectric/jWwWjQ23wHI/X9b_ZdDMJg0J
cached-robolectric-classes.jar
。如果您按照Robolectric的Eclipse安装说明进行操作,则此文件将位于[您的主要Android项目] / tmp。