Mocking private classes

3,855 views
Skip to first unread message

Dusica Radojevic

unread,
Aug 21, 2011, 1:50:43 PM8/21/11
to PowerMock
Hi,

I looked at documentation related to Mocking Private, but I didn't
find anything about mocking instatiation of private class inside
method. Can you refer to some example?

Thanks.
Dusica

Johan Haleby

unread,
Aug 21, 2011, 4:39:07 PM8/21/11
to powe...@googlegroups.com
Can you give us an example of what you're trying to mock?

/Johan

Dusica Radojevic

unread,
Aug 21, 2011, 9:18:34 PM8/21/11
to powe...@googlegroups.com
Actually, I kinda found a way to do it, using reflection. Problem that I had was how to set class type for mocking object(it is private class inside another class), so I get declared classes inside the parent class and comparing class names defined the one I needed.
Exactly, it was like this:

The class I tested extends one Spring's class. Concrete problem happened at point when I had to mock line from Spring's parent class super method, which was:

StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props);

inside method "processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)" in class "PropertyPlaceholderConfigurer", where "PlaceholderResolvingStringValueResolver" is a private class inside "PropertyPlaceholderConfigurer", so I used next:

Class clazz = Class.forName("org.springframework.beans.factory.config.PropertyPlaceholderConfigurer");
Class privateClazz = null;
for (int i = 0; i < clazz.getDeclaredClasses().length; i++) {
if (clazz.getDeclaredClasses()[i].getName()
.equals("org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver")){
privateClazz = clazz.getDeclaredClasses()[i]; 
                        break;
}
}

And later I used "privateClazz" to define type in method createMock().
It works for me.

Maybe this is not elegant and the best way to do it, but I didn't find anything other useful.
I hope this helps in some way..

Dusica


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


Johan Haleby

unread,
Aug 22, 2011, 4:11:07 AM8/22/11
to powe...@googlegroups.com
Hi,

I would strongly advice against mocking private inner classes since usually there are other more elegant ways of testing your problem and the test case can end up being very complex. Personally I would try to refactor the code if the test gets too complex or write an integration test instead of mocking at all.

PowerMock do include methods to find the type of private inner classes, including anonymous inner classes. It's undocumented though mainly because it should be used with extreme care. You can have a look at an example here.

Regards,
/Johan
To unsubscribe from this group, send email to powermock+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/powermock?hl=en.

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages