Erico
Erico

Reputation: 711

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!

Upvotes: 21

Views: 10784

Answers (7)

hlidka
hlidka

Reputation: 2345

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)
        }
    }
}

Upvotes: 3

Valery M
Valery M

Reputation: 89

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) 

Upvotes: 1

Wirling
Wirling

Reputation: 5375

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)

Upvotes: 5

Pankaj Jangid
Pankaj Jangid

Reputation: 9

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

Upvotes: -1

rsssssonsss
rsssssonsss

Reputation: 423

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

Upvotes: 0

Erico
Erico

Reputation: 711

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.

Upvotes: 3

Alex Mamo
Alex Mamo

Reputation: 138824

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'

Upvotes: 1

Related Questions