让Robolectric与Volley合作

时间:2013-05-29 14:27:22

标签: android robolectric android-volley

我想让Volley与Robolectric合作。我可以看到我的HTTP请求被调用,并且调用了parseNetworkResponse(我发送了JsonRequest的自定义子类),但我的监听器没有被调用。有什么建议?这是一个代码示例:

@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));
        }
    }
}

2 个答案:

答案 0 :(得分:19)

我通过将RequestQueue的ResponseDelivery替换为不使用Looper.getMainLooper()但使用新Executor的ResponseDelivery来解决同样的问题。示例代码:

public static RequestQueue newRequestQueueForTest(final Context context, final OkHttpClient okHttpClient) {
    final File cacheDir = new File(context.getCacheDir(), "volley");

    final Network network = new BasicNetwork(new OkHttpStack(okHttpClient));

    final ResponseDelivery responseDelivery = new ExecutorDelivery(Executors.newSingleThreadExecutor());

    final RequestQueue queue =
            new RequestQueue(
                    new DiskBasedCache(cacheDir),
                    network,
                    4,
                    responseDelivery);

    queue.start();

    return queue;
}

注意:使用Robolectric-2.2-SNAPSHOT,之前的版本与Volley并不配合。

希望这有帮助

答案 1 :(得分:0)

受@Thomas Moerman的回答启发,我创建了这个课程:

public class RealRequestQueue {

    public static Builder newBuilder() {
        return new Builder();
    }

    public static final class Builder {
        private Cache mCache;
        private Network mNetwork;

        private Builder() {
        }

        public Builder cache(Cache val) {
            mCache = val;
            return this;
        }

        public Builder network(Network val) {
            mNetwork = val;
            return this;
        }

        public RequestQueue build() {
            if (mNetwork == null) mNetwork = new BasicNetwork(new HttpStack() {
                @Override public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
                    return null;
                }
            });
            if (mCache == null) {
                Context context = RuntimeEnvironment.application.getApplicationContext();
                mCache = new DiskBasedCache(new File(context.getCacheDir(), "volley"));
            }

            ResponseDelivery responseDelivery = new ExecutorDelivery(Executors.newSingleThreadExecutor());
            final RequestQueue queue = new RequestQueue(mCache, mNetwork, 4, responseDelivery);

            return queue;
        }
    }
}

然后我监视请求队列并将其注入被测系统

mQueue = spy(RealRequestQueue.newBuilder().network(mNetwork).build());