2012-08-03 104 views
2

我試圖使用Robolectric測試框架的HTTP layer測試HTTP調用。Robolectric HTTP測試在NoSuchMethodError上失敗:org.apache.http.impl.client.DefaultRequestDirector

我的測試類已經使用RunWith(RobolectricTestRunner.class)進行了註釋。下面是一個示例HTTP測試案例:

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) 

任何想法可能會導致:

@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將()調用) Apache類拋出異常?

使用當前最新的庫,robolectric-1.2-20120730.165026-113-jar-with-dependencies.jar。

回答

5

發現了Robolectric谷歌組答案: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並重新開始工作。