java - Is it possible to have conditional mocking of methods that take no arguments? -


mockito masters, have challenge you! ;)

i have method takes no arguments , mock behaviour provide different results, depending on external conditions.

for instance, i'd this:

myinterface mymock = mock(myinterface.class); sky sky = buildrandomskycolor();  when(mymock.methodwithnoarguments()).thenreturn("blue").if(sky.isblue()); when(mymock.methodwithnoarguments()).thenreturn("grey").if(sky.isgrey()); 

is possible have conditional kind of behaviour on mockito? i've tried using dostub() , doanswer(), got nowhere.

any appreciated! lot!

you use custom answer this

myinterface mymock = mock(myinterface.class); sky sky = buildrandomskycolor();  when(mymock.methodwithnoarguments()).thenanswer(new answer<string>(){     string answer(invocationonmock invocation) {         if(sky.isblue())             return "blue";         else             return "gray";     } } 

Comments