2

我正在尝试使用 Robolectric 测试框架的HTTP 层来测试 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 失败跟踪报告以下 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。

4

1 回答 1

5

在 Robolectric Google Group 上找到答案:https ://groups.google.com/forum/#!msg/robolectric/jWwWjQ23wHI/X9b_ZdDMJg0J

  1. 右键单击 Robolectric Java 测试项目 > 属性 > Java 构建路径 > 订购和导出。
  2. 将 robolectric jar 移动到库列表的顶部。
  3. 查找并删除cached-robolectric-classes.jar. 如果您按照 Robolectric 的 Eclipse 安装说明进行操作,此文件将位于 [您的主要 Android 项目]/tmp 下。
  4. 重新运行 JUnit 并重新开始工作。
于 2012-08-03T17:17:23.853 回答