0

EasyMock 3.0 documentation (http://www.easymock.org/EasyMock3_0_Documentation.html) claims that it is, but doesn't explain why (emphasis mine):

Sometimes you may need to mock only some methods of a class and keep the normal behavior of others. This usually happens when you want to test a method that calls some others in the same class. So you want to keep the normal behavior of the tested method and mock the others.

In this case, the first thing to do is to consider a refactoring since most of the time this problem caused by a bad design.

So, is this correct? And why?

Asker
  • 105
  • 8

2 Answers2

2

The problem is not calling own methods, but rather the necessity to mock those calls is indicative of another problem. Otherwise you can just test the public methods and verify their behavior. You shouldn't need to care about what other methods that one calls, but only about the observable behavior.

koljaTM
  • 10,064
  • 2
  • 40
  • 42
1

As always, it depends.

Methods call other methods all the time; it's a natural consequence of (IMO proper) refactoring, slicing other methods down to size. The issue for me comes at the point where you have to mock those tiny proper methods and cannot test without doing so.

If there are a ton of little methods it's possible that they're more appropriately wrapped in a collaborator class of some sort, at which point you'd mock the collaborator, instead of the internal methods.

Also, those little internal methods might be private methods, the testing of which is a subject of debate.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302