Is there a way to match any class argument of the below sample routine?
class A {
public B method(Class extends A> a) {}
}
How
the solution from millhouse is not working anymore with recent version of mockito
This solution work with java 8 and mockito 2.2.9
where ArgumentMatcher is an instanceof org.mockito.ArgumentMatcher
public class ClassOrSubclassMatcher implements ArgumentMatcher> {
private final Class targetClass;
public ClassOrSubclassMatcher(Class targetClass) {
this.targetClass = targetClass;
}
@Override
public boolean matches(Class obj) {
if (obj != null) {
if (obj instanceof Class) {
return targetClass.isAssignableFrom( obj);
}
}
return false;
}
}
And the use
when(a.method(ArgumentMatchers.argThat(new ClassOrSubclassMatcher<>(A.class)))).thenReturn(b);