【问题标题】:How would you write a test for this method using Mockito?您将如何使用 Mockito 为这种方法编写测试?
【发布时间】:2015-07-16 10:22:51
【问题描述】:

请协助。我似乎无法为此方法创建合适的测试:

protected void startInterfacing() {

    mLiveAuthClient.login(mView.context(), Arrays.asList(SCOPES), new LiveAuthListener() {

        @Override
        public void onAuthComplete(final LiveStatus liveStatus, final LiveConnectSession liveConnectSession,
                                   Object o) {

            // Login successful and user consented, now retrieve user ID and connect with backend server
            getUserIdAndConnectWithBackendServer(liveConnectSession, mLiveAuthClient);
        }

        @Override
        public void onAuthError(LiveAuthException e, Object o) {
            // We failed to authenticate with auth service... show error
            if (e.getError().equals("access_denied") ||
                    e.getMessage().equals("The user cancelled the login operation.")) {

                // When user cancels in either the login or consent page, we need to log the user out to enable
                // the login screen again when trying to connect later on
                logUserOut(mLiveAuthClient, false);
            } else {
                onErrorOccured();
            }
        }
    });
}

我将解释一下这里发生了什么: 我正在尝试验证我的客户端并登录 OneDrive。 该方法从调用 Live SDK 的登录方法开始。该 SDK 对象是从此类外部提供给我的。所以我基本上可以嘲笑它。 这就是我正在努力解决的问题:

  1. 我不需要测试对登录方法的调用,因为它不是我的。我确实需要在 onAuthComplete 中测试对 getUserIdAndConnectWithBackendServer() 的调用。但是这个方法需要一个 liveConnectSession 对象。我该如何提供?它是在 onAuthComplete 方法上给我的。

  2. 如何模拟对 onAuthComplete 和 onAuthError 的调用?我阅读了有关 ArgumentCaptor 的信息,但是当我使用它时,我需要在调用实际方法时为这些方法提供参数。 例如,argument.getValue().onAuthComplete() 要求我为此调用添加参数。我在这里实际提供了什么?

这是下一个大致相同但有其自身问题的方法:

 protected void getUserIdAndConnectWithBackendServer(final LiveConnectSession liveConnectSession, final LiveAuthClient
        authClient) {

    final LiveConnectClient connectClient =  new LiveConnectClient(liveConnectSession);

    connectClient.getAsync("me", new LiveOperationListener() {
        @Override
        public void onComplete(LiveOperation liveOperation) {

            // We got a result. Check for errors...
            JSONObject result = liveOperation.getResult();
            if (result.has(ERROR)) {
                JSONObject error = result.optJSONObject(ERROR);
                String code = error.optString(CODE);
                String message = error.optString(MESSAGE);
                onErrorOccured();
            } else {
                connectWithBackend(result, liveConnectSession, authClient);
            }
        }

        @Override
        public void onError(LiveOperationException e, LiveOperation liveOperation) {
            // We failed to retrieve user information.... show error
            onErrorOccured();
            logUserOut(authClient, false);
        }
    });
}

在这里,我想模拟一下 JSONObject。但是如何调用 onComplete 方法或 onError 方法。我将提供什么作为方法提供给我的论据。比如 LiveOperation?

谢谢!!

【问题讨论】:

    标签: android unit-testing mockito


    【解决方案1】:

    我最终使用的解决方案是使用 mockito 的 doAnswer() 结构。 这使我能够获取回调参数并调用其方法之一。 另一种解决方案是使用 ArgumentCator。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 2017-12-19
      相关资源
      最近更新 更多