Reputation: 439
When I use Easymock(or a similar mocking framework) to implement my unit tests, I'm forced to do interaction-based testing (as I don't get to assert on the state of my dependencies. Or am I mistaken?).
On the other hand if I use a hand written stub (instead of using easymock) I can implement state based testing.
I'm quite unclear if I want to go with interaction based testing or state based testing.
I'm biased and I want to use Easymock, but I'm not sure if there would be any side-effects that I may have to face in the future.
Can anyone please throw some light on this?
Thanks in advance!
Upvotes: 1
Views: 355
Reputation: 246
You have to divide your objects into domainy value objects (which hold state and should be immutable) and services. Services are the things other objects should ask to perform a particular task, but your code shouldn't be concerned about how this task is performed. To test the service in isolation without testing its peers, use a mock.
Value objects, which may contain domain functionality such as calculations, should never be mocked, because their responsibility is calculating and not delegating.
In a well designed system, services should always be injected and never returned from other services, so generally speaking, mocks shouldn't return mocks.
Upvotes: 4
Reputation: 91931
There is no reason you cannot do both. I find behavior-based or interaction-based testing using mocks saves a lot of boilerplate when all you want to do is test behavior. With hand-written stubs you end up with a lot of booleans indicating that a method was called that you have to then test for. That is redundant, brittle and quite a drag.
On the other hand, sometimes you do want to test state. For example, if the object under test's behavior needs to change based on the state of the candidate for mocking or stubbing, and there is some complex interaction to work out.
In that case, mocking frameworks can get in the way, and a hand-written stub makes managing the state much easier for the purposes of the test.
So the bottom line is that they are not mutually exclusive - use what makes sense for a given test. As long as each test is small and tests only one thing (as much as is reasonable) then you shouldn't find yourself in a situation where you started with a mock an suddenly find you have to do a bunch of effort to get things back to a stub.
Upvotes: 0