java - Why some hardcoded values in testing affects others? -


i'm trying figure out how write unit tests , stuck @ 1 point:

suppose have 2 tests (view , service mocked):

@test public void shouldstartmainactivitywhenusernameandpassowordarecorrect() throws exception{     when(view.getusername()).thenreturn("james");     when(view.getpassword()).thenreturn("bond"); //bond     when(serivce.login("james", "bnd")).thenreturn(true);  //bnd gives error      presenter.onloginclicked();      verify(view).startmainactivity();  }  @test public void shouldshowerrormessangewhenusernameandpasswordareincorrect() throws exception{     when(view.getusername()).thenreturn("james");     when(view.getpassword()).thenreturn("bond"); // bond     when(serivce.login("james", "bnd")).thenreturn(false); // bnd gives no error      presenter.onloginclicked();      verify(view).showcredentialserror(); } 

this presenters method:

public void onloginclicked() {     string username = view.getusername();     if (username.isempty()){         view.showusernameerror(r.string.username_error);         return;     }     string password = view.getpassword();     if (password.isempty()){         view.showpassworderror(r.string.password_error);     }     boolean loginsucceeded = service.login(username, password);     if (loginsucceeded){         view.startmainactivity();     }else{         view.showcredentialserror();     }  } 

how comes "bnd" hardcoded value compared "bond" hardcoded value method?

for example:

when(view.getpassword()).thenreturn("bond"); //bond when(serivce.login("james", "bnd")).thenreturn(true); //bnd 

this give me error, while changing

when(view.getpassword()).thenreturn("bond"); //bond when(serivce.login("james", "bond")).thenreturn(true); //bond 

gives no error.

you have mocked 2 scenarios when service returns boolean saying if login successful or not.

you have following mocks:

  1. mock view.getusername() returns james. whenever method invoked on test class unit test return value.

  2. mock view.getpassword() returns bond. whenever method invoked on test class unit test return value.

  3. mock serivce.login() takes 2 arguments i.e. {"username","password"} , returns login status i.e. true or false. whenever method invoked on test class unit test same set of parameters return these value.

in first test case returning false mock username: james , password: bnd , in second test returning true same set of parameters.

when(serivce.login("james", "bnd")).thenreturn(true); //bnd gives error

here returning true {"james", "bnd"}.

when(serivce.login("james", "bnd")).thenreturn(false); // bnd gives no error

here returning false {"james", "bnd"}.

right mock not getting executed in either of 2 cases. because username , password in presenters class takes values first 2 mocks i.e. view.getusername() , view.getpassword(). these mocks returning james , bonds respectively in both test cases.

logically should returning true or false test cases if , if arguments {"james","bonds"} , not else. validated parameters returned other 2 mocks , based on whether matches or not, third mock executed , return boolean.

positive scenario:

when(view.getusername()).thenreturn("james"); when(view.getpassword()).thenreturn("bond"); when(serivce.login("james", "bond")).thenreturn(true); /* corrected */ 

negative scenario:

when(view.getusername()).thenreturn("james"); when(view.getpassword()).thenreturn("bond"); when(serivce.login("james", "bond")).thenreturn(false); /* corrected */ 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -