久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

單元測試網絡響應.在調試時工作,而不是在實際

Unit testing a network response. Works when debugging, not when actually running(單元測試網絡響應.在調試時工作,而不是在實際運行時工作)
本文介紹了單元測試網絡響應.在調試時工作,而不是在實際運行時工作的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我目前正在嘗試測試是否確實收到了網絡響應.

I am currently attempting to test that a network response is actually being received.

雖然我知道這不是我在測試方面應該做的事情,但這是我自己的好奇心,如果可能的話,我想繼續.

就目前而言,我已經成功創建了測試.請求被毫無問題地發送到排球隊列.

As it stands, I have successfully created the test. A request is sent to a volley queue without issue.

現在奇怪的部分:

該請求永遠不會執行.這是我如何測試它的想法:

That request is never executed. Here's an idea of how I'm testing it:

 @Test
    public void testSimpleGetResponseFromServerVolley() throws Exception {
        final CountDownLatch signal = new CountDownLatch(1);

        NetworkClass.NetworkListener listener = new NetworkClass.NetworkListener() {
            @Override
            public void onResponse(Response response) {
                assertThat(response != null);
                System.out.println("Got Response");
                signal.countDown();

            }

            @Override
            public void onError(Throwable error) {
                System.out.println("No Response");
                signal.countDown();
            }
        };
        NetworkClass.getResponseFromServer(null, listener);
        signal.await();
    }

此代碼意外導致測試掛起并且永遠無法完成.

This code unexpectedly causes the test to hang and never complete.

然而,這是我不再對情況失去理解的地方:

如果我通過 debug 運行測試并逐行執行,則測試成功執行,并收到響應.

If I run the test via debug and step through line by line, the test successfully executes, and response is received.

我認為正在發生的事情:

當我通過調試單步執行時,volley requestQueue 成功進行并發出請求,并在調用 await() 之前收到響應.

When I step through via debug, the volley requestQueue successfully carries on and makes the request, and the response is received before await() is called.

當我不通過調試單步執行時,await() 會阻塞處理所有這些的線程.

When I don't step through via debug, await() is blocking the thread which handles all of that.

關于如何處理這個問題的任何想法?

Any ideas on how I can handle this?

推薦答案

Volley 依賴 Looper.getMainLooper() 來處理其執行.當使用 RobolectricTestRunner 時,Robolectric 會對此進行模擬,因此它不會正確設置,從而導致測試失敗.

Volley relies on Looper.getMainLooper() to handle its executions. When using a RobolectricTestRunner, Robolectric mocks this out and as such it will not be correctly set up, thus resulting in a failed test.

在我的具體情況下,當我使用斷點時,系統實際上確實設置了主循環器,因為系統正在利用它來顯示斷點/調試工具.因此,這描述了我最初的問題中發現的行為背后的原因.

In my specific case, when I was using breakpoints, the system actually does set up the main looper, because the system is utilizing it to show the breakpoints / debugging tools. This thus describes the reasoning behind the behaviour found in my initial question.

現在,對于在單元測試期間使用 Volley 獲得真實網絡響應的解決方案,必須將執行程序更改為不使用主循環器.

Now for a solution as to getting real network responses with Volley during a unit test, the executor must be changed to not be using the main looper.

作為一個簡單的解決方案,創建一個依賴于 Executor.singleThreadExecutor() 而不是 Main Looper 的請求隊列.

As an easy solution, create a request queue that relies on Executor.singleThreadExecutor() instead of the Main Looper.

這就是我的意思:

    //Specific test queue that uses a singleThreadExecutor instead of the mainLooper for testing purposes.
public RequestQueue newVolleyRequestQueueForTest(final Context context) {
    File cacheDir = new File(context.getCacheDir(), "cache/volley");
    Network network = new BasicNetwork(new HurlStack());
    ResponseDelivery responseDelivery = new ExecutorDelivery(Executors.newSingleThreadExecutor());
    RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network, 4, responseDelivery);
    queue.start();
    return queue;
}

然后,在測試期間將其用作 Volley 的請求隊列.

Then, use that as your request queue for Volley during the tests.

這里的關鍵是:

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

希望這會有所幫助!

這篇關于單元測試網絡響應.在調試時工作,而不是在實際運行時工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event(EditText:禁用文本選擇處理程序單擊事件上的粘貼/替換菜單彈出)
Multiline EditText with Done SoftInput Action Label on 2.3(2.3 上帶有完成 SoftInput 操作標簽的多行 EditText)
How to detect the swipe left or Right in Android?(如何在 Android 中檢測向左或向右滑動?)
Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋轉對話框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設置為僅接受 Android 中的數值?)
主站蜘蛛池模板: 亚洲视频一区在线观看 | 亚洲精品久久久一区二区三区 | 国产一区二区三区www | 亚洲一区毛片 | 久久国产电影 | 黄色骚片 | 99色在线 | 精品一区二区三区在线视频 | 国产精品123区 | 日韩精品视频在线 | 国产精品自拍视频 | 91精品国产99 | 国产乱肥老妇国产一区二 | 欧美精品二区 | 国产精品免费看 | 91精品一区二区三区久久久久 | 成人h视频在线 | 久久精品色欧美aⅴ一区二区 | 久久精品国产99国产精品 | 精品久久久精品 | 日韩中文在线视频 | 欧美日本免费 | 国产亚洲一区二区三区在线观看 | 超碰精品在线 | 97日日碰人人模人人澡分享吧 | 午夜一级黄色片 | 在线观看黄视频 | 欧美一级片在线看 | 亚洲欧美中文日韩在线v日本 | 精品欧美一区二区三区久久久小说 | 成人欧美一区二区三区黑人孕妇 | 国产成人午夜高潮毛片 | www.887色视频免费 | 伊人网91 | 免费观看毛片 | 国产精品揄拍一区二区久久国内亚洲精 | 网站国产 | 小草久久久久久久久爱六 | 亚洲福利网站 | av日韩高清 | 一级美国黄色片 |