简体   繁体   中英

How to unit test Firestore in android using JUnit?

I've code below:

class SimpleTest {
    @Test
    fun observable_isPass() {
        val store = FirebaseFirestore.getInstance()
        assert(true)
    }
}

When I try to run the test, it's throw exception below:

 Method getMainLooper in android.os.Looper not mocked.

How to test the firestore data in unit test?

Probably a late answer, but Firestore uses callbacks that aren't on the main thread. In order to unit test, API's such as Retrofit provide methods to execute calls on main threads but as far as I know Firestore doesn't provide such flexibility. Here is a work around to pause the main thread until a failure or success callback returns a response from Firestore.

val db = Firebase.firestore

val latch = CountDownLatch(1)

db.collection("test1").document("data")
    .set(
    // Data
    ).addOnFailureListener {
            // assertions here
            latch.countDown()
    }.addOnSuccessListener {
            // assertions here
            latch.countDown()
    }
// Will wait for the latch to become 0
latch.await()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM