How to verify that something implements an interface with Mockito

If you want to write a unit test which verifies that something implements a particular interface, or extends a particular class, here’s how…

I recently wanted to use Mockito to verify that a particular object passed to some method implements a given interface. I noticed that the anyOf Hamcrest matcher inspects the exact type of the given object and therefore fails if it doesn’t find an exact match:


 
verify().that(someMock.someMethod(anyOf(SomeInterface))); // FAILS

This hampers refactoring to interfaces and polymorphism somewhat. The simple solution was to use a custom matcher with argThatMatches:

private function isType(expected:Class, actual:*):Boolean
{
    return actual is expected;
}

verify().that(someMock.someMethod(argThatMatches(SomeInterface, isType))); // PASSES

#WIN