EasyMock -- mock methods within tested class?

Jeremy picture Jeremy · Nov 27, 2012 · Viewed 14.5k times · Source

In my code I sometimes call public or private methods within the same class. These methods aren't a good candidate for being pulled out into their own class. Each of these methods that I call are tested in their own unit test.

So, if I have a method in my class A that calls each of those methods also in class A, is there some way to mock the calls? I can certainly cut and paste my expectations/mock behavior, but not only is that tedious, it obfuscates the point of the test, violates modularity, and makes testing more difficulty because of the inability to control what is returned.

If not, what is the usual solution to this kind of thing?

Answer

ach picture ach · Nov 27, 2012

It sounds like you're looking for Partial Mocks... here's one blog post that covers them: http://www.jroller.com/alessiopace/entry/partial_mocks_with_easymock

This requires the EasyMock ClassExtension, which unfortunately can't mock private methods however.