Unit testing 如何使用JUnit在android中对Firestore进行单元测试?

Unit testing 如何使用JUnit在android中对Firestore进行单元测试?,unit-testing,junit,google-cloud-firestore,Unit Testing,Junit,Google Cloud Firestore,我的代码如下: class SimpleTest { @Test fun observable_isPass() { val store = FirebaseFirestore.getInstance() assert(true) } } 当我尝试运行测试时,抛出异常如下: 如何在单元测试中测试firestore数据?可能回答得比较晚,但firestore使用的回调不在主线程上。为了进行单元测试,改型等API提供了在主线程上执行调用的

我的代码如下:

class SimpleTest {
    @Test
    fun observable_isPass() {
        val store = FirebaseFirestore.getInstance()
        assert(true)
    }
}
当我尝试运行测试时,抛出异常如下:


如何在单元测试中测试firestore数据?

可能回答得比较晚,但firestore使用的回调不在主线程上。为了进行单元测试,改型等API提供了在主线程上执行调用的方法,但据我所知,Firestore并没有提供这种灵活性。以下是暂停主线程的变通方法,直到失败或成功回调从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()
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()