Wednesday, October 3, 2007

Don't Use Mocks...

...when you aren't really interested in testing the interactions. This is related to my previous post regarding mocks and stubs. If leaving off the verification of mocks wouldn't change the nature of what you are testing, then you aren't writing an interaction-based test. Therefore, the mock dependency probably creates more noise than simply using a simple stub. If your dependency is not behind an interface, you can in most cases still create a stub using subclass and override.

While writing:

Dependency dependency = createMock(Dependency.class);
ThingToTest target = new ThingToTest(dependency);

isn't too bad. It still causes more noise than:

ThingToTest target = new ThingToTest(new DependencyStub());

Most of the time, I see that stub and grok the fact that in the context of this test, I really don't care what the Class under test does with that dependency.

No comments: