43

I have a method:

void putObject(<T extends BaseEntity> param)

Have some test where I mock this method, but I wonder, how to verify that method was called with parameter of particular class? Tried to do it in such ways:

verify(foo).putObject((SomeClass)anyObject());
------
verify(foo).putObject(any(SomeClass.class));
------
ArgumentCaptor<SomeClass> parameter = ArgumentCaptor
            .forClass(SomeClass.class);
verify(foo).putObject(parametr);

Works only the second variant with any(), but it doesnt check class ofparameter`. So if it is possible to verify that method get any object of particular class?

2 Answers 2

89

User Matcher documentation isA() method.

public static <T> T isA(java.lang.Class<T> clazz)

That will solve your problem.

verify(foo).putObject(isA(SomeClass.class));

0
6

Actually you can check with ArgumentCaptor.

ArgumentCaptor<SomeClass> parameterCaptor = ArgumentCaptor
            .forClass(SomeClass.class);
verify(foo).putObject(parameterCaptor.capture());

SomeClass instance = parameterCaptor.getValue();
// check intance.getX()
// check instance.getY()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.