java - Can Mockito stub a method without regard to the argument? -


i'm trying test legacy code, using mockito.

i want stub foodao used in production follows:

foo = foodao.getbar(new bazoo()); 

i can write:

when(foodao.getbar(new bazoo())).thenreturn(myfoo); 

but obvious problem getbar() never called same bazoo object stubbed method for. (curse new operator!)

i love if stub method in way returns myfoo regardless of argument. failing that, i'll listen other workaround suggestions, i'd avoid changing production code until there reasonable test coverage.

when(   foodao.getbar(     any(bazoo.class)   ) ).thenreturn(myfoo); 

or (to avoid nulls):

when(   foodao.getbar(     (bazoo)notnull()   ) ).thenreturn(myfoo); 

don't forget import matchers (many others available):

import static org.mockito.matchers.*; 

Comments

Popular posts from this blog

haskell - Using filter on an item in a list? -

c# - When does PreApplicationStartMethod actually get triggered to run? -

tomcat6 - Exception when stopping container for a with Spring + Quartz + Tomcat web application -