JUnit testing on class with firebase

為{幸葍}努か 提交于 2019-12-01 10:52:23

As usual, I suggest everybody to not mix presentation and storage code. And this is a question for another topic.

And here the trick how you can achieve what you want. First, extract method for Firebase initialisation and providing FirebaseAuth:

 @VisibleForTest
 @NonNull
 FirebaseAuth initAndReturnFirebaseAuth() {
     FirebaseApp.initializeApp(this);
     FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
 }

Second, create test activity and override this method:

public class TestWeekListActivity extends WeekListActivity {
    @Override
    @NonNull
    FirebaseAuth initAndReturnFirebaseAuth() {
       FirebaseAuth authMock = mock(FirebaseAuth.class);
       when(authMock.getCurrentUser()).thenReturn(mockFirebaseUser);
       return authMock;
    }
}

And then use test activity in test instead of you real activity.

Hope it helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!