Pritam Sangani
Pritam Sangani

Reputation: 301

Spring Boot + Cloud Firestore Integration Tests

I have written unit tests for my service, repository and controller for adding and retrieving data from a Cloud Firestore database, mocking the addition and retrieval of data from a mock firestore object. However, I want to write and retrieve actual data from an actual Cloud Firestore instance, without interacting with my firestore instances in my staging and production environments. Is there any way of creating a local development instance of Cloud Firestore?

Upvotes: 0

Views: 1188

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138989

mocking the addition and retrieval of data from a mock Firestore object.

There is no way you can get the data only from a mock Firestore object. So instead of mocking only the Firestore object, you'll have to also mock any other CollectionReference or DocumentReference object that is returned as a result of calling collection() or document() in your chain. For instance, if you call collection() on your Firestore object, the object that is returned is a CollectionReference object which should be mocked. Furthermore, if you call then in turn document() on the resulted CollectionReference object, the object that is returned is of type DocumentReference, which should also be mocked. So this mock operation should be done for every new such method call.

This is happening because the mocks do not know how to generate more mock objects for the methods on that mock. As explained above, you have to tell it what to return for each method call individually.

Upvotes: 1

Related Questions