Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test - FirebaseApp is not initialized in this process

I'm trying to run a test with Robolectric and it has an integration with Firebase. I have a project MyProject - Test that I'll be using to run the tests in a real instance of the database.

The problem is that, when running cleanup before test, I have the error:

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process null. Make sure to call FirebaseApp.initializeApp(Context) first.

at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
at com.cgbros.silkhub.activity.listener.LoginActivityUnitTest.cleanUp(LoginActivityUnitTest.kt:26) <28 internal calls>

The test file: https://gist.github.com/augustoerico/e88d3e5b59ae5d023d83c114b8ffa708 (I tried to copy-paste the source here. Failed miserably...)

Any insights on how I can make this work?

Thank you!

like image 241
Erico Avatar asked Nov 12 '17 21:11

Erico


2 Answers

Ok, I've figured it out. It's actually a conceptual problem. The problem is that I'm trying to use the Firebase Android SDK to run a test outside of the Android context. I mean, my test is not an Android application, and I'm trying to use the Android SDK. All FirebaseApp.initializeApp() need a Context as a parameter.

So, what I have to do is to find a way to interact with my FirebaseDatabase. I think I can go with firebase-admin, or maybe use this one: https://github.com/bane73/firebase4j

Thank you for taking the time to help me (:

** Update **

I've opted to go with the Firebase REST API https://firebase.google.com/docs/reference/rest/database/

For future reference, I'm giving public full access, by tweaking the rules, so it is easier to write the tests setup and cleanup.

like image 105
Erico Avatar answered Nov 09 '22 02:11

Erico


I've had the same problem and fixed it by initializing Firebase in the before step of my unit test:

FirebaseApp.initializeApp(InstrumentationRegistry.getInstrumentation().targetContext)
like image 3
Wirling Avatar answered Nov 09 '22 03:11

Wirling