您将如何使用 Mockito 为该方法编写测试?
How would you write a test for this method using Mockito?
请为此提供帮助。我似乎无法为此方法创建合适的测试:
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 对象是从 class 外部提供给我的。所以我基本上可以嘲笑它。
这就是我正在努力解决的问题:
我不需要测试对登录方法的调用,因为它不是我的。我确实需要在 onAuthComplete 中测试对 getUserIdAndConnectWithBackendServer() 的调用。但是这个方法需要一个 liveConnectSession 对象。我该如何提供?它是通过 onAuthComplete 方法提供给我的。
如何模拟对 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?
谢谢!!
我最终使用的解决方案是使用mockito的doAnswer()结构。
这使我能够获取回调参数并调用其方法之一。
另一种解决方案是使用 ArgumentCator。
请为此提供帮助。我似乎无法为此方法创建合适的测试:
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 对象是从 class 外部提供给我的。所以我基本上可以嘲笑它。 这就是我正在努力解决的问题:
我不需要测试对登录方法的调用,因为它不是我的。我确实需要在 onAuthComplete 中测试对 getUserIdAndConnectWithBackendServer() 的调用。但是这个方法需要一个 liveConnectSession 对象。我该如何提供?它是通过 onAuthComplete 方法提供给我的。
如何模拟对 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?
谢谢!!
我最终使用的解决方案是使用mockito的doAnswer()结构。 这使我能够获取回调参数并调用其方法之一。 另一种解决方案是使用 ArgumentCator。