久久久久久久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 中的數值?)
主站蜘蛛池模板: 久久最新精品 | 国产精品一区二区在线播放 | 日韩在线视频精品 | 91在线网站 | 婷婷色国产偷v国产偷v小说 | 欧美在线网站 | 九九热视频这里只有精品 | 高清人人天天夜夜曰狠狠狠狠 | caoporn国产精品免费公开 | 亚洲一区二区三区在线 | 亚洲日本欧美日韩高观看 | 天堂精品视频 | www精品美女久久久tv | 99国内精品 | 特级生活片 | 最新日韩在线视频 | 国产精品欧美一区二区三区 | 黄一区二区三区 | 九九九久久国产免费 | 狠狠色综合久久丁香婷婷 | 欧美一级www片免费观看 | 中文字幕亚洲精品在线观看 | 罗宾被扒开腿做同人网站 | 久久久久国产精品一区 | 成人三级网址 | 在线观看 亚洲 | av免费网址 | 日韩一级二级片 | 日韩一区二区三区在线观看 | 综合二区 | 成人h免费观看视频 | 日韩精品一区二区三区免费视频 | www.欧美 | 亚洲视频中文字幕 | 国产精品亚洲综合 | 精品久久99 | 亚洲精品日韩综合观看成人91 | 青青草国产在线观看 | 午夜av免费 | 国产精品视频网站 | 一区二区小视频 |