我想讓Volley和Robolectric一起工作。我可以看到我的HTTP請求被調用,parseNetworkResponse被調用(我發送了JsonRequest的自定義子類),但是我的Listener沒有被調用。有什麼建議?下面是一個代碼示例:讓Robolectric和Volley一起工作
@Test
public void testTypeAheadClient() throws Exception {
Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
//mRemoteRequestQueue and mCustomRequest are set up previously
mRemoteRequestQueue.add(mCustomRequest);
}
private static class CustomRequest extends JsonRequest<MyObject> {
public CustomRequest(String url,
Response.Listener<MyObject> listener,
Response.ErrorListener errorListener) {
super(Request.Method.GET, url, null, listener, errorListener);
}
@Override
protected Response<MyObject> parseNetworkResponse(NetworkResponse response) {
System.out.println("in parseNetworkResponse");
try {
MyObject myObject = new MyObject(new JSONArray(new String(response.data, "UTF-8")));
return Response.success(myObject, HttpHeaderParser.parseCacheHeaders(response));
} catch (Exception e) {
e.printStackTrace();
return Response.error(new ParseError(e));
}
}
}
正如你使用回調,我想這是一個異步庫。要在Android Async Http庫中使用Robolectric,您需要設置您自己的線程池。也許在這種情況下,可以採用相同的解決方案。 https://groups.google.com/forum/#!msg/robolectric/Z3Yg04gL4hg/iwYlnvHAkO4J – Axxiss
如果你有這個工作,你可以發佈你的整個測試代碼? –