vendredi 8 janvier 2021

Mock Firebase Authentication using Mockito

I'm using FirebaseAuth in my Java project and I would like to do some tests. I don't want to test the Firebase class itself (obviously I think) but a Service class which implements a Firebase method call.

So the code is like:

@Service
public class MyServiceImpl implements MyService

  public MyServiceImpl() {}

  public void deleteUser(String uid){
    // do something
    FirebaseAuth.getInstance().deleteUser(uid); // <-- I want to mock this call
  }

So I'm running JUnit tests where I mock DAO methods call into MyServiceImpl.java using @Spy. But I don't know how to mock FirebaseAuth instance and its methods.

I`ve tried:

@Test
public void mockFirebase(){
  FirebaseAuth auth mockFirebase = Mockito.mock(FirebaseAuth.class);
  Mockito.when(mockFirebase.getInstance()).thenReturn(/* null ?? */);
}

But it try to connect with Firebase I think because I deleted all configuration to avoid connection and test fail in the first line with result.

FirebaseApp with name [DEFAULT] doesn't exist.

Also I have read some questions to use Mockito with Firestore or FirebaseDatabase but none with FirebaseAuth.

At this point I don't know how to mock delete() method, if I have to create a mock class using @Spy (like DAO layer) or I have to mock FirebaseAuth class entire or whatever.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire