android - Retrofit 中的单元测试以进行回调

标签 android unit-testing junit mockito retrofit2

我在这里得到了演示者中的代码示例。我如何在改造调用中为 onSuccess 和 onFailure 编写测试

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

}

我如何编写单元测试来覆盖这段代码的所有情况。

谢谢

最佳答案

当您想测试来自服务 (API) 的不同响应时,最好模拟它并返回您需要的内容。

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

关于android - Retrofit 中的单元测试以进行回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45160055/

相关文章:

android - 自定义 AlertDialog 列表布局

c# - 单元测试 FileSystemWatcher.Error 事件

java - 对具有同一项目的不同版本的所有子目录运行 JUnit 测试

java - Gradle 不执行测试

android - 如何使用git跟踪android分支

java - while 循环导致 Activity 屏幕什么都不显示?

android - 在 Eclipse 中使用 Android 5.0 版本的 CardView

hibernate - 在单元测试中使用Java 8 Time API的Grails 3.1.1

.net - 我们将理论属性用于什么?

java - AspectJ + Junit + Maven - 在测试中识别切入点但 NoSuchMethodError : aspectOf() exception thrown