BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News ObjectMother - a Forgotten Testing Tool

ObjectMother - a Forgotten Testing Tool

One of the earliest techniques for writing tests using TDD did not use mocks and stubs, but used the actual business objects instead. By creating a set of factories that instantiated, composed, and executed methods on business objects, real objects, in a non-initial-state of their lifecycle, could be created for testing purposes. The name coined for this pattern was ObjectMother.

This technique has never caught on as widely as mocks and stubs, but it has never died out either. Developers are rediscovering it constantly when they start to feel that mocking/stubbing is too difficult because of breaking dependencies, or fragile because they are a form of duplication that must be maintained in parallel to the real business objects.

JB Rainsberger used this type of solution for testing a Rails application in preparation for XP Day Toronto 2007. Maarten Winkels considers ObjectMother a viable alternative when testing the Data Access Layer. And Vikas Kerni dropped mocks because he felt they were too much overhead and ended up using ObjectMother instead.

At the same time, many consider ObjectMother a bad idea, or even an anti-pattern because of its (mis)use. J. Richardson left this comment on the c2 wiki page for ObjectMother:

Actually this should be called an AntiPattern. In practice all those I've seen that have advocated this approach return shamefaced a few months later. This pattern is a bit like the other well known pattern "putting everything in one place", and what it is that is actually being created is quite non-obvious. A Builder pattern might be more useful...
In the end, ObjectMother is one more tool in the testing toolbox. What is your experience - is it worth using and if so, in what context? Or is it dangerous and evil - and if so, why?

Rate this Article

Adoption
Style

BT