18

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!

Erico
  • 629
  • 1
  • 6
  • 14
  • When I last had this (earlier this morning) I'd forgotten to add `apply plugin: 'com.google.gms.google-services'` to the end of my app's `build.gradle`. Also have a look at some of the previous questions about this error: https://stackoverflow.com/search?q=Default+FirebaseApp+is+not+initialized+in+this+process+null – Frank van Puffelen Nov 12 '17 at 21:45

7 Answers7

5

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)
Wirling
  • 4,810
  • 3
  • 48
  • 78
  • It worked! I initialized Firebase in my tests as you mentioned. I was trying to get the instance at onCreate FirebaseCrashlytics.getInstance().recordException(loggedException); – dianakarenms Nov 17 '21 at 23:13
3

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.

Erico
  • 629
  • 1
  • 6
  • 14
1

I also had the same issue a few day ago and i solve it in the same way the error says. You get that error because you're trying to get an instance of Firebase without initialize it. Please add this line of code before you try to get an instance of Firebase like this:

FirebaseApp.initializeApp(this);

Also, as Franks says, please be sure you have the following line of code added in your build.gradle file, as the last line of your entire file.

apply plugin: 'com.google.gms.google-services'
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thank you for your answer. The problem I'm facing is that I'd like to initialize Firebase in my test class, so I can populate it and clean it up. So, I can't just use "this" because it is not an instance of Context. – Erico Nov 14 '17 at 04:42
  • That's why s happening this. You cannot instantiete Firebase without an instance of Context. In this case create a method in your TestClass with the argument of Context. Then just call that method from the ActivityClass using as an argument an instance of Context. Will work for sure. – Alex Mamo Nov 14 '17 at 12:38
  • Thank you for taking the time to help me (: I think I was just trying to use the Firebase SDK in the wrong way, I think I should be using the REST API to populate the database and clean it up. Thank you! – Erico Nov 14 '17 at 16:57
1

I used the most simple and straightforward approach. Since you init your Firebase App in your Application, just create a variable there. For example,

//Your Application.class
open var isTestMode = false

onCreate(){
if(!isTestMode){
//Init your Firebase app
}
}

And then in Robolectric Application just extend your application and override the variable. In this case don't forget to provide configuration

@Config(application = TestApplication::class) 
Valery M
  • 89
  • 3
1

The @Before approach did not fix the problem for me, because Firebase was called during app initialization. I was able to fix with this instead:

MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        if (BuildConfig.DEBUG) {
            if (Build.FINGERPRINT == "robolectric") {
                FirebaseApp.initializeApp(this)
            }
            logger.w("Using Firestore emulator")
            val firestore = Firebase.firestore
            firestore.useEmulator("192.168.1.102", 8080)
        }
    }
}
hlidka
  • 2,086
  • 1
  • 15
  • 14
0

I also faced the above issue. Do all plugin download and checks by nuget. But in the end, I found the crashing was due to , Application Package Name not matching the one Assigned in Google Dev. Application package name must matche the one that assigned in the Google Dev. Console

rsssssonsss
  • 413
  • 5
  • 6
-1

after add this line and then you get error

FirebaseApp.initializeApp(this);

then you have to do that also

add this line to Gradle (App)

apply plugin: 'com.google.gms.google-services'

add this line to Gradle (Project)

classpath 'com.google.gms:google-services:3.2.1'

also check if no dependncy is missing and make sure you add google-services.json