【发布时间】:2018-03-17 18:57:57
【问题描述】:
我正在单元测试一个类 - AuthenticationService - 负责使用 Firebase Auth 对用户进行身份验证。为此,我正在使用 JUnit、Mockito 和 PowerMock。
我完全是在模拟 Firebase 身份验证,因为我的主要目标是类中包含的逻辑。我的问题在于这种方法:
public void loginWithEmailAndPassword(String email, String password, OnCompletedListener listener) {
if (Strings.isNullOrEmpty(email) || !Pattern.compile(EMAIL_PATTERN).matcher(email).matches()) {
throw new IllegalArgumentException("email field is empty or bad formatted");
}
if (Strings.isNullOrEmpty(password)) {
throw new IllegalArgumentException("password field must be not empty");
}
mFirebaseAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
if (listener != null)
listener.onComplete(new AuthResult(true, null));
} else {
Exception exception = (FirebaseAuthException)task.getException();
Log.e(TAG, exception.getMessage());
if (listener != null) {
AuthResult result = new AuthResult(false, createStatusFromFirebaseException(exception));
listener.onComplete(result);
}
}
});
}
我想测试 addOnCompleteListener 中传递的 lambda 方法。 我知道我需要以某种方式调用这个 lambda 方法,因为 Firebase 本身永远不会调用它,毕竟我是在嘲笑 Firebase。
问题是:我不知道如何在我的单元测试代码中调用这个 lambda 方法。 我需要测试是否在监听器中调用了 onComplete 方法,以及它的参数。
提前致谢。
【问题讨论】:
标签: android unit-testing firebase mockito powermock