I am attempting to implement testing for an android app that I'm building. One of the tests involves an interactor file I wrote (DatabaseInteractor.java
) that connects to Google Firestore. The default constructor sets the FirestoreFirebase
object to FirebaseFirestore.getInstance();
.
I am using Mockito & JUnit for my unit tests. When I attempt to create a DataBaseInteractor object in my Unit Test, I get the following error:
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process null. Make sure to call FirebaseApp.initializeApp(Context) first.
Apparently this error occurs when the default constructor is called in my Unit Test. I'm not sure why this is happening. I am able to create a DataBaseInteractor object in other parts of my android app without issue. Are there perhaps limitations to when and where I can initialize a class that utilizes FireBaseFireStore
objects? If I want to mock the behavior of FireBase for unit testing, how do I get around these restrictions?
EDIT: A truncated version of my DataBaseInteractor.java
file:
public class DatabaseInteractor {
private static User theUser;
private FirebaseFirestore db;
private DocumentReference userData;
public DatabaseInteractor() {
db = FirebaseFirestore.getInstance();
theUser = new User();
}
// ... various methods that add/retrieve Users from FireStore ... //
}
Mockito is a Java-based framework used for unit testing of Java applications. This mocking framework helps in the development of testable applications.
JUnit and Mockito can be primarily classified as "Testing Frameworks" tools. JUnit and Mockito are both open source tools. It seems that Mockito with 9.02K GitHub stars and 1.62K forks on GitHub has more adoption than JUnit with 7.53K GitHub stars and 2.8K GitHub forks.
Mockito is a mocking framework. It is a Java-based library used to create simple and basic test APIs for performing unit testing of Java applications. It can also be used with other frameworks such as JUnit and TestNG.
You need to inject the FirebaseFirestore instance in your constructor so that it can be mocked, something like:
public class DatabaseInteractor {
private static User theUser;
private FirebaseFirestore db;
private DocumentReference userData;
public DatabaseInteractor(FirebaseFirestore firestore) {
db = firestore
theUser = new User();
}
// ... various methods that add/retrieve Users from FireStore ... //
}
The in your test you can mock FirebaseFirestore
with Mockito:
@Test
public void someTest() {
FirebaseFirestore mockFirestore = Mockito.mock(FirebaseFirestore.class)
Mockito.when(mockFirestore.someMethodCallYouWantToMock()).thenReturn(something)
DatabaseInteractor interactor = new DatabaseInteractor(mockFirestore)
// some assertion or verification
}
I'd suggest you read about dependency injection, it will make unit testing a lot easier if you can pass in mocks of your dependencies in the constructor instead of instantiating them inside it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With