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

回調改造中的單元測試

Unit Testing in Retrofit for Callback(回調改造中的單元測試)
本文介紹了回調改造中的單元測試的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在這里,我得到了演示者中的代碼示例.如何在改造調用中為 onSuccess 和 onFailure 編寫測試

Here i got a sample of code in presenter. How do i make write a test for onSuccess and onFailure in retrofit call

public void getNotifications(final List<HashMap<String,Object>> notifications){

        if (!"".equalsIgnoreCase(userDB.getValueFromSqlite("email",1))) {
            UserNotifications userNotifications =
                    new UserNotifications(userDB.getValueFromSqlite("email",1),Integer.parseInt(userDB.getValueFromSqlite("userId",1).trim()));
            Call call = apiInterface.getNotifications(userNotifications);
            call.enqueue(new Callback() {
                @Override
                public void onResponse(Call call, Response response) {
                    UserNotifications userNotifications1 = (UserNotifications) response.body();


                    if(userNotifications1.getNotifications().isEmpty()){
                        view.setListToAdapter(notifications);
                        onFailure(call,new Throwable());
                    }
                    else {
                        for (UserNotifications.Datum datum:userNotifications1.getNotifications()) {
                            HashMap<String,Object> singleNotification= new HashMap<>();
                            singleNotification.put("notification",datum.getNotification());
                            singleNotification.put("date",datum.getDate());
                            notifications.add(singleNotification);
                        }
                        view.setListToAdapter(notifications);
                    }
                }

                @Override
                public void onFailure(Call call, Throwable t) {
                    call.cancel();
                }
            });
        }
    }

}

我如何編寫單元測試來涵蓋這段代碼的所有情況.

How do i write unittesting to cover all cases for this piece of code.

謝謝

推薦答案

當您想測試來自服務 (API) 的不同響應時,最好模擬它并返回您需要的內容.

When you want to test different responses from service (API) it's probably best to mock it and return what you need.

    @Test
    public void testApiResponse() {
      ApiInterface mockedApiInterface = Mockito.mock(ApiInterface.class);
      Call<UserNotifications> mockedCall = Mockito.mock(Call.class);

      Mockito.when(mockedApiInterface.getNotifications()).thenReturn(mockedCall);

      Mockito.doAnswer(new Answer() {
        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
          Callback<UserNotifications> callback = invocation.getArgumentAt(0, Callback.class);

          callback.onResponse(mockedCall, Response.success(new UserNotifications()));
          // or callback.onResponse(mockedCall, Response.error(404. ...);
          // or callback.onFailure(mockedCall, new IOException());

          return null;
        }
      }).when(mockedCall).enqueue(any(Callback.class));

      // inject mocked ApiInterface to your presenter
      // and then mock view and verify calls (and eventually use ArgumentCaptor to access call parameters)
    }

這篇關于回調改造中的單元測試的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 中的數值?)
主站蜘蛛池模板: 久久久久久久国产 | 欧美精品1区 | 成人精品免费视频 | 久久国产精品91 | 亚洲高清网| 日本韩国电影免费观看 | 精品亚洲一区二区三区四区五区 | 日韩精品免费在线 | 国产精品色 | 国产成人精品一区 | 国产精品毛片一区二区三区 | 男女羞羞免费视频 | 成人av在线网站 | 免费看91| 欧美日韩国产不卡 | 国产激情视频在线观看 | 亚洲精品久久久久久下一站 | 国产在线网站 | 亚州国产 | 久久久久国产精品 | 精品一区二区三区在线视频 | 国产在线一区二区三区 | 国产免费视频 | 欧美成人第一页 | 国产精品一区二区在线免费观看 | 中文字幕日韩专区 | 日皮视频免费 | 亚洲一区二区三区四区五区中文 | 亚洲综合色婷婷 | av中文在线 | 国产一区二区三区久久久久久久久 | 久久国产激情视频 | 久久久久久国产精品 | 黄色一级大片在线免费看产 | 亚洲狠狠 | 成人免费一级视频 | 日韩电影免费在线观看中文字幕 | 7777精品伊人久久精品影视 | 男人天堂99 | 一级黄色片日本 | 1级毛片 |