Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何验证方法是否在模拟类中运行';她回电话了吗?_Android_Unit Testing_Mocking_Mockito_Firebase Authentication - Fatal编程技术网

Android 如何验证方法是否在模拟类中运行';她回电话了吗?

Android 如何验证方法是否在模拟类中运行';她回电话了吗?,android,unit-testing,mocking,mockito,firebase-authentication,Android,Unit Testing,Mocking,Mockito,Firebase Authentication,我的Android代码中有一个实用程序类,用于处理用户身份验证。我正在使用Mokcito为这个类编写单元测试,以验证是否在创建新用户成功或失败时通知侦听器。以下是此实用程序类的方法之一: public void createNewUser(String email, String password) { firebaseAuth.createUserWithEmailAndPassword(email, password) .addOnSuccessListene

我的Android代码中有一个实用程序类,用于处理用户身份验证。我正在使用Mokcito为这个类编写单元测试,以验证是否在创建新用户成功或失败时通知侦听器。以下是此实用程序类的方法之一:

public void createNewUser(String email, String password) {
    firebaseAuth.createUserWithEmailAndPassword(email, password)
            .addOnSuccessListener(authResult -> {
                authListener.newUserCreated();
            })
            .addOnFailureListener(e -> {
                authListener.failedCreatingNewUser();
            });
}
我正在模拟
FirebaseAuth
,我想验证是否调用了
authlister.newUserCreated()
。我曾尝试使用深存根和参数捕获器来处理
firebaseAuth.createUserWithEmailAndPassword上的链式方法调用,但我不知道如何使其工作

更新

下面是我的测试类,其中包含此方法的测试:

public class AuthUtilsTest {

    private static final String USERNAME = "USERNAME";
    private static final String PASSWORD = "PASSWORD";

    @Mock
    private FirebaseAuth firebaseAuth;

    @Mock
    private FirebaseFirestore firebaseFirestore;

    @Mock
    private BaseEncoding base64;

    @Mock
    private PreferencesRepo preferencesRepo;

    @Mock
    private AuthUtilsContract.EventListener eventListener;

    private AuthUtils authUtils;

    @Before
    public void setupAuthUtils() {
        MockitoAnnotations.initMocks(this);

        authUtils = new AuthUtils(
                preferencesRepo,
                firebaseAuth,
                firebaseFirestore,
                base64
        );

        authUtils.takeEventListener(eventListener);
    }

    @Test
    public void failureCreatingNewUserTellsListener() {
        Task<AuthResult> failedTask = Tasks.forException(new Exception("fail"));
        when(firebaseAuth.createUserWithEmailAndPassword(anyString(), anyString())).thenReturn(failedTask);

        authUtils.createNewUser(USERNAME, PASSWORD);

        verify(eventListener).failedCreatingNewUser();
    }

}
公共类AuthUtilsTest{
私有静态最终字符串USERNAME=“USERNAME”;
私有静态最终字符串PASSWORD=“PASSWORD”;
@嘲弄
私有FirebaseAuth FirebaseAuth;
@嘲弄
私人FirebaseFirestore FirebaseFirestore;
@嘲弄
专用base64编码;
@嘲弄
私人优先股优先股优先股优先股;
@嘲弄
private AuthUtilsContract.EventListener EventListener;
私人授权人;
@以前
public void setupauthils(){
initMocks(this);
authUtils=新的authUtils(
优先权,
firebaseAuth,
firebaseFirestore,
base64
);
takeEventListener(eventListener);
}
@试验
public void failureCreatingNewUserTellsListener(){
Task failedTask=Tasks.forException(新异常(“失败”);
当(firebaseAuth.createUserWithEmailAndPassword(anyString(),anyString()))。然后返回(failedTask);
authUtils.createNewUser(用户名、密码);
验证(eventListener).failedCreatingNewUser();
}
}
这引发了异常

java.lang.ExceptionInInitializerRor位于 com.google.android.gms.tasks.zzn.addOnSuccessListener(未知源) ... 原因:java.lang.RuntimeException:中的方法getMainLooper android.os.Looper没有被嘲笑


使用Mockito.when使createUserCall返回已完成的
任务


然后Mockito.验证authListener是否执行了假设authListener也是一个mock时应该执行的操作

使用Mockito.when使createUserCall返回模拟任务。 然后在任务上执行Mockito.verify以捕获addlistener调用的参数

根据您的意愿测试捕获的参数(这类似于单元测试中的单元测试,捕获的参数是您正在测试的新类)

此方法实际上不会测试是否调用了侦听器。只是调用了addlistener方法,回调在调用时做了应该做的事情

verify(mockTask).addOnSuccessListener(listenerCaptor.capture());
OnSuccessListener<Auth> newObjectUnderTest = listenerCaptor.getValue();

//ACT
newObjectUnderTest.onSuccess(auth);

//ASSERT
verify(authListener).newUserCreated();
verify(mockTask.addOnSuccessListener(listenerCaptor.capture());
OnSuccessListener newObjectUnderTest=listenerCaptor.getValue();
//表演
newObjectUnderTest.onSuccess(auth);
//断言
验证(authListener).newUserCreated();

我已经用我尝试的解决方案更新了主要帖子。你能用一个代码示例更新你的帖子吗?