firebasefirebase-realtime-databasegoogle-tasks-apiandroid-junit

Unit test stops after addOnCompleteListener of the Firebase Task API


I've implemented my first testcase for my App. Before every testcase, I need to login using Firebase.

I've programmed everything in my Firebase class using the Tasks API. The Firebase class is asynchronuous but sends back a Task, and the result can be read with addOnCompleteListener. However, after logging in, logUserIn(), the test stops and "someNumber" does not get print. I cannot put the logUserIn() method in a @Before method because it is asynchronuous.

@RunWith(AndroidJUnit4::class)
class NetworthServiceTest {

    @Rule
    @JvmField
    val rule = ActivityTestRule(MainActivity::class.java)

    private fun logUserIn(): Task<Boolean> {
        val tcs = TaskCompletionSource<Boolean>()

        val task = FirebaseService().initialize(
                TEST_USER_EMAIL,
                TEST_USER_PASSWORD,
                rule.activity)

        task.addOnCompleteListener {
            Assert.assertEquals("User " + TEST_USER_EMAIL + " cannot be logged in", "", it.result.second)

            val loginManager = UtilsProvider().getBookyLoginManager()
            Assert.assertEquals("User should be logged in", true, loginManager.isLoggedIn())

            tcs.setResult(true)
        }

        return tcs.task
    }

    @Test
    @Throws(Exception::class)
    fun whenReadingTableShouldGive10Results() {
        val task = logUserIn()
        task.addOnCompleteListener {
            val someNumber = 10
            Log.d("","print nr = "+ someNumber)
        }
    }
}


class FirebaseService {

    fun initialize(email: String, password: String, activity: Activity): Task<Pair<Any, String>> {
        database = FirebaseDatabase.getInstance().reference
        auth = FirebaseAuth.getInstance()

        val tcs = TaskCompletionSource<Pair<Any, String>>()

        auth!!.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(activity) { task ->
                    if (task.isSuccessful) {
                        user = auth!!.currentUser
                        isInitialized = true
                        val pair = Pair<Any, String>(null, "")
                        tcs.setResult(pair)
                    } else {
                        val pair = Pair<Any, String>(null, "error: " + task.exception!!.message)
                        tcs.setResult(pair)
                    }
                }
        return tcs.task
    }
}

Solution

  • You can use Tasks.await() to block the current thread to wait for the completion of a task. This is very much not recommended for application code (read more about that in this blog), but should be OK for tests.