Verify methods called with Method.invoke()

1,300 views
Skip to first unread message

Patricio Arvizu

unread,
Jan 11, 2010, 10:35:37 AM1/11/10
to mockito
I've tried this but it initially didn't work, maybe I'm missing
something so I'll try again, but I thought I would post this in the
meantime; if it is not a bug, it would be a nice feature request :)

Can I verify methods called with Method.invoke()?

A simplified version of my code would be something along these lines:

public class MyClass
{

public void callMethod(Object callbackObject, String methodName)
{
Class<?> theClass = callbackObject.getClass();
method = theClass.getMethod(methodName);

method.invoke(callbackObject);
}
}

public MyClassTest
{
public void testCallMethod()
{
Object mockObject = mock(Object.class);
String testMethod = "toString()";

MyClass class = new MyClass();

class.callMethod(mockObject, testMethod);

verify(mockObject).toString();
}
}

Should this work?

Thanks,
-Pato

Patricio Arvizu

unread,
Jan 11, 2010, 6:24:46 PM1/11/10
to mockito
It actually did work when I used a class other than Object, does this
sound right?

-Pato

szczepiq

unread,
Jan 11, 2010, 7:03:10 PM1/11/10
to moc...@googlegroups.com
Hi,

It should work just fine. Not sure what you mean by class other than
Object. Mockito verifications / stubbing will work fine for:
- invoking methods explicitly (foo.bar())
- invoking methods via reflection: Foo.class.getMethod("bar").invoke(....

Cheers,
Szczepan Faber

> --
> You received this message because you are subscribed to the Google Groups "mockito" group.
> To post to this group, send email to moc...@googlegroups.com.
> To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mockito?hl=en.
>
>
>
>

Patricio Arvizu

unread,
Jan 12, 2010, 10:59:26 AM1/12/10
to mockito
I tried it with java.lang.Number and it worked fine.

Now, I know there is something special about stubbing or verifying
toString(); could that be cause?

-Pato

On 11 ene, 18:03, szczepiq <szcze...@gmail.com> wrote:
> Hi,
>
> It should work just fine. Not sure what you mean by class other than
> Object. Mockito verifications / stubbing will work fine for:
>  - invoking methods explicitly (foo.bar())
>  - invoking methods via reflection: Foo.class.getMethod("bar").invoke(....
>
> Cheers,
> Szczepan Faber
>

szczepiq

unread,
Jan 13, 2010, 12:17:37 AM1/13/10
to moc...@googlegroups.com
Correct, you cannot verify toString()

Cheers!
Szczepan Faber

Reply all
Reply to author
Forward
0 new messages