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 null
s):
when( foodao.getbar( (bazoo)notnull() ) ).thenreturn(myfoo);
don't forget import matchers (many others available):
import static org.mockito.matchers.*;
Comments
Post a Comment