September 21, 2021

Firebase | Firestore | Firestore junit test | deep stubbing mockito

This is a sample code how to perform deep stubbing mockito on firestore
you need to mock all the 


public class FirestoreClientTest{

@Mock
Firestore firestore;

@Mock
DocumentReference documentReference;

@Mock
CollectionReference collectionReference;

@Mock
ApiFuture apiFuture;

@Mock
QuerySnapshot querySnapshot;

@Test
public void test(){
when(firestore.collection(any(String.class)).thenReturn(collectionReference);
when(collectionReference.document(any(String.class)).thenReturn(documentReference);
when(documentReference.get()).thenReturn(apiFuture);
when(apiFuture.get()).thenReturn(querySnapshot);

UserInfo userInfo = firestore.getUserDetails();

//Assert statements
}

}

September 14, 2021

Google Firebase Firestore Spring boot Integration sample code

1. Create a new Cloud Firestore database in your GCP project

2. If you are authenticated in the Cloud SDK, your credentials will be automatically found by the Spring Boot Starter for Google Cloud Firestore.

3. Pom dependency

<!-- https://mvnrepository.com/artifact/com.google.cloud/spring-cloud-gcp-starter -->
<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter</artifactId>
    <version>2.0.4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-firestore -->
<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-firestore</artifactId>
    <version>3.0.1</version>
</dependency>



4. Repository (writing data)

@Autowired
Firestore firestore;

User data = new User("javabelazy",
Arrays.asList(
new Phone(12345, PhoneType.CELL),
new Phone(54321, PhoneType.WORK)));

WriteResult writeResult = this.firestore.document("users/id").set(data).get();

5. Application.properties

spring.cloud.gcp.firestore.project-id =abcdefg

6. Git URL

Facebook comments