'Ignore firebase related exceptions in Roboelectric unit test
My Roboelectric unit tests are giving this exception when I try to initialise Firebase in the Application class.
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process null. Make sure to call FirebaseApp.initializeApp(Context) first.
Is there any way where I can ignore this exceptions and get my tests pass?
Solution 1:[1]
If you do not need firebase in your test, you can annotate the test class with
@RunWith(RobolectricTestRunner.class)
@Config(application = Application.class)
to use a different application from your app custom application.
Solution 2:[2]
As an option you can use mockStatic from mockito:
mockStatic(FirebaseFirestore::class.java)
.use { firebaseFirestore ->
firebaseFirestore.`when`<Any> { FirebaseFirestore.getInstance() }.thenReturn(mock(FirebaseFirestore::class.java))
}
Solution 3:[3]
Organize your code so that it isn't reliant on a Firebase connection and pass in a mock? Its generally not a good idea to have unit tests rely on a remote service, that's more the realm of integration tests or acceptance tests.
Or alternatively- do the initialization in an @Before if that's too hard?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Makibo |
Solution 2 | walkmn |
Solution 3 | Gabe Sechan |