I'm trying to test an Rx wrapper I made around fir...
# rx
s
I'm trying to test an Rx wrapper I made around firebase Auth create method. I've tried using an argument captor. I just picked up mockito over the weekend. How do I test multiple async call backs with rxjava and mockito?
k
mock the network call and use
Schedulers.trampoline()
a
I think we're going to need some more code. If you're calling through to firebase in, say, an
Observable.create
call you'll need a way to separate out firebase from your testing.
s
Copy code
object RxAuth {

    fun createUser(firebaseAuth : FirebaseAuth, email : String, password : String): Maybe<AuthResult> {
        return Maybe.create<AuthResult> {emitter: MaybeEmitter<AuthResult> ->
            firebaseAuth.createUserWithEmailAndPassword(email, password).addOnSuccessListener {
                if (!emitter.isDisposed) {
                    emitter.onSuccess(it)
                }

            }.addOnFailureListener {
                if (!emitter.isDisposed){
                    emitter.onError(it)
                }

            }.addOnCompleteListener{
                if (!emitter.isDisposed) {
                    emitter.onComplete()
                }
            }

        }
    }
}
Copy code
@RunWith(MockitoJUnitRunner::class)
class FirebaseRepositoryImpTest {

    @get:Rule
    val instantTaskExecutorRule = InstantTaskExecutorRule()

    @Mock
    lateinit var firebaseDatabase: FirebaseDatabase

    @Mock
    lateinit var firebaseAuth: FirebaseAuth

    @Mock
    lateinit var authResultTask: Task<AuthResult>

    @Mock
    lateinit var authResult: AuthResult

    @Mock
    lateinit var firebaseUser: FirebaseUser

    private lateinit var firebaseRepositoryImp: FirebaseRepositoryImp


    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)

        this.firebaseRepositoryImp = FirebaseRepositoryImp(firebaseDatabase, firebaseAuth)

        RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
        RxJavaPlugins.setComputationSchedulerHandler { Schedulers.trampoline() }
        RxJavaPlugins.setNewThreadSchedulerHandler { Schedulers.trampoline() }
        RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }

    }

    @Test
    fun signUp_whenUserCreateAccount_UserAccountIsCreatedSuccessfully() {
        val onSuccessListenerCaptor: KArgumentCaptor<OnSuccessListener<AuthResult>> =
            argumentCaptor()

        val onCompleteListenerCaptor: KArgumentCaptor<OnCompleteListener<AuthResult>> =
            argumentCaptor()


        `when`(
            firebaseAuth.createUserWithEmailAndPassword(
                TestUtil.getTestUserCrendential().email,
                TestUtil.getTestUserCrendential().password
            )
        ).thenReturn(authResultTask)

        val authTestObserver = firebaseRepositoryImp.signUp(
            TestUtil.getTestUserCrendential().email,
            TestUtil.getTestUserCrendential().password
        ).test()

        verify(authResultTask).addOnSuccessListener(onSuccessListenerCaptor.capture())
        verify(authResultTask).addOnCompleteListener(onCompleteListenerCaptor.capture())


        onSuccessListenerCaptor.firstValue.onSuccess(authResult)
        onCompleteListenerCaptor.firstValue.onComplete(authResultTask)

        verify(firebaseAuth).createUserWithEmailAndPassword(
            TestUtil.getTestUserCrendential().email,
            TestUtil.getTestUserCrendential().password
        )

        authTestObserver.assertNoErrors()
            .assertValueCount(1)
            .assertValueSet(Collections.singletonList(authResult)).dispose()
    }

    @After
    fun tearDown() {
    }
}
Copy code
Wanted but not invoked:
authResultTask.addOnCompleteListener(
    <Capturing argument>
);
-> at com.google.android.gms.tasks.Task.addOnCompleteListener(Unknown Source)

However, there was exactly 1 interaction with this mock:
authResultTask.addOnSuccessListener(
    com.devsanmiaderibigbe.tasks.data.remote.RxAuth$createUser$1$1@16ecee1
);
-> at com.devsanmiaderibigbe.tasks.data.remote.RxAuth$createUser$1.subscribe(RxAuth.kt:12)
Please those are the codes and the error message. I am trying to test the rxjava wrapper