android - Robolectric app testing with Firebase -
i'm trying write simple robolectric test presenter, uses firebase database , firebase auth. every time i'm trying start test, throwes illegalstateexception.
java.lang.illegalstateexception: firebaseapp name [default] doesn't exist. @ com.google.firebase.firebaseapp.getinstance(unknown source) @ com.google.firebase.firebaseapp.getinstance(unknown source) @ com.google.firebase.auth.firebaseauth.getinstance(unknown source)
my test quite simple
@runwith(robolectrictestrunner.class) @config(constants = buildconfig.class) public class loginpresentertest { private loginpresenter presenter; private loginmvpview view; @before public void beforeeachtest() { presenter = new loginpresenter(); view = new loginfragment(); } @test public void attachview_shouldattachviewtothepresenter() { presenter.attachview(view); assertsame(presenter.getmvpview(), view); } }
while in presenter constructor firebase instances.
public loginpresenter() { this.firebaseauth = firebaseauth.getinstance(); this.database = firebasedatabase.getinstance().getreference(); }
is there way use robolectric firebase?
if don't use them in code test possible inject them constructor:
public loginpresenter(firebaseauth firebaseauth, firebasedatabase database){ this.firebaseauth = firebaseauth; this.database = database; }
and inject null
them, remember poor way using null
. better way use library mockito or use interfaces/wrapper etc.
e.g. use interface
public interface idatabase { public list<string> getdata(); }
the loginpresenter
:
public loginpresenter(firebaseauth firebaseauth, idatabase database){ this.firebaseauth = firebaseauth; this.database = database; }
the normal implementation of idatabase
:
public class mydatabase implements idatabase { private firebasedatabase database; public mydatabase(firebasedatabase database) { this.database = database; } public list<string> getdate() { // use firebasedatabase returning getdata return ...; } }
and easy mock database using idatabase
:
public class databasemock implements idatabase { public list<string> getdata() { // return expected data mock return ...; } }
call test like:
presenter = new loginpresenter(firebaseauth.getinstance(), new databasemock());
Comments
Post a Comment