mocking - Using Mockito, how do I test a 'finite loop' with a boolean condition? -
using mockito, how test 'finite loop' ?
i have method want test looks this:
public void dismisssearchareasuggestions() { while (aresearchareasuggestionsvisible()) { clicksearchareafield(); sleeper.sleeptight(costtestbase.half_second); } }
and, want test first 2 calls 'aresearchareasuggestionsvisible' return true, so:
mockito.when(mockelementstate.iselementfoundandvisible(landingpage.address_suggestions, testbase.one_second)).thenreturn(boolean.true);
but, third call false.
mockito.when(mockelementstate.iselementfoundandvisible(landingpage.address_suggestions, testbase.one_second)).thenreturn(boolean.false);
how mockito in 1 single test method?
here test class far:
@test public void testdismisssearchareasuggestionswhenvisible() { mockito.when(mockelementstate.iselementfoundandvisible(landingpage.address_suggestions, costtestbase.one_second)).thenreturn(boolean.true); landingpage.dismisssearchareasuggestions(); mockito.verify(mockelementstate).iselementfoundandvisible(landingpage .address_suggestions, costtestbase.one_second); }
as long make all of stubs part of same chain, mockito proceed through them in sequence, repeating final call.
// returns false, false, true, true, true... when(your.mockedcall(param))' .thenreturn(boolean.false, boolean.false, boolean.true);
you can syntax...
// returns false, false, true, true, true... when(your.mockedcall(param)) .thenreturn(boolean.false) .thenreturn(boolean.false) .thenreturn(boolean.true);
...which can come in handy if actions aren't return values.
// returns false, false, true, throws exception when(your.mockedcall(param)) .thenreturn(boolean.false) .thenreturn(boolean.false) .thenreturn(boolean.true) .thenthrow(new exception("called many times!"));
if want things more complex that, consider writing answer.
Comments
Post a Comment