How can I invoke method by string name?

37 views
Skip to first unread message

Eugeny Shchelkanov

unread,
Oct 12, 2011, 10:33:15 AM10/12/11
to mockito
My situation is: I am loading an OSGi bundle to Equinox, loading a
class from bundle and I want to test it. I need to mock a class so
that bundle's class will accept it. I can't use a class from my
sources because it will not be the same as bundle's class. Must be
confusing, so I will make an example.

Class MAIN uses class LOGIC which uses external resources, so I want
to mock it. I load dynamically (from OSGi bundle, using
Bundle.loadClass()) Class objects for MAIN and LOGIC. I am creating
mock for LOGIC.I can't cast dynamically created object to LOGIC: they
are loaded with different classloaders. I am getting a
ClassCastException. I can't write:
verify(mockLOGIC).method();
because mockLOGIC is of type Object and simply does not have this
method.

Can I somehow specify method name as a String? I did not find anything
similar in javadoc. If Mockito can't do this, can you suggest mocking
framework that can?

No refactoring possible, I am testing interaction with JCICS.

Szczepan Faber

unread,
Oct 13, 2011, 1:27:42 PM10/13/11
to moc...@googlegroups.com
Hey,

Mockito does not support verification by String. At the moment, it's not even in plans because it's not refactoring safe and it's going to be quite a challenge to fit it into the current mocking DSL :)

One of the option is to make sure you use the same classloader ;) I wonder if you can have a slightly different bundle (or extra bundles) for testing purposes.

I don't know which mocking tools support mocking type-lessly by string but I'm sure there are some.

Cheers!


--
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.




--
Szczepan Faber
Principal engineer@gradleware
Lead@mockito

Eugeny Shchelkanov

unread,
Oct 14, 2011, 2:24:24 AM10/14/11
to mockito
Hi,

I have already found that PowerMock can do this. Thanks.
Reply all
Reply to author
Forward
0 new messages