Search code examples
androidkotlinfirebase-realtime-databasejunit

How to use await in JUnit cleanup?


In my cleanup function I would like to clear Firebase Realtime Database (emulator).. but to be sure it has succeeded before it goes to the next test, I have to call await..

@After
fun cleanup() {
   ...
   instance.reference.setValue(null).await()
}

However, the cleanup function can not be suspend function.. so how could I achieve this?

thanks!


Solution

  • I usually delete nodes and records after query for them, but I guess you can remove the top default root node

    Get the reference

          MyFireBaseReference firebaseDbRef = FirebaseDatabase.getInstance().getReference();
    

    then Delete the db or root node

          firebaseDbRef.removeValue();
    

    A sample, please adapt to your needs.. documentation help from Kotlin for you

      fun main() = runBlocking { // this: CoroutineScope
    
      launch { // launch a new coroutine and continue
        
        println("Blocking and cleaning up DB!") // print after delay
    
          // get your DB reference
           MyFireBaseReference firebaseDbRef = FirebaseDatabase.getInstance().getReference();
    
          // then Delete the db or root nod
          firebaseDbRef.removeValue();
    
     }
     println("Hello") // main coroutine continues while a previous one is delayed
    }