Search code examples
javaunit-testingmockitohamcrest

Why does hamcrest any(Myclass.class) requires casting


I usually use Hamcrest like this:

doNothing().when(entityManagerMock).persist(any(Point.class));

then I have tried to write the same syntax on this:

doNothing().when(locationTagsMock).persistLocationTag(any(LocationTag.class));

But I got compilation error which forced me to re-factor my code to this:

doNothing().when(locationTagsMock).persistLocationTag((LocationTag) any(LocationTag.class));

Why is the casting needed all the sudden?

How can I avoid it, if at all?


Solution

  • According to Doppelganger's comment on the answer to Using Mockito's generic "any()" method, you've got a conflict between hamcrest's any() and mockito's any().