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

Can Mockito stub a method without regard to the argument?


when(  fooDao.getBar(    any(Bazoo.class)  )).thenReturn(myFoo);

or (to avoid nulls):

when(  fooDao.getBar(    (Bazoo)notNull()  )).thenReturn(myFoo);

Don't forget to import matchers (many others are available):

For Mockito 2.1.0 and newer:

import static org.mockito.ArgumentMatchers.*;

For older versions:

import static org.mockito.Matchers.*;


Use like this:

when(  fooDao.getBar(    Matchers.<Bazoo>any()  )).thenReturn(myFoo);

Before you need to import Mockito.Matchers


http://site.mockito.org/mockito/docs/1.10.19/org/mockito/Matchers.html

anyObject() should fit your needs.

Also, you can always consider implementing hashCode() and equals() for the Bazoo class. This would make your code example work the way you want.