An interesting article about Test data builders
Imagine the situation of testing a typical website. You are trying to test PageX, but first you have to create a user and login. But pageX is completely different whether the user is a new visitor, existing member, or an admin. So you need to create all three users. You create three scripts that whip through the registration (or bypassit ) and run your tests. Eventually you decide you want to refactor into a factory so you can do UserFactory.createMemberUser(), etc.
But then you need to test PageX for paying members but not for free members. Complexity is spiraling out of control (okay, maybe not in this simple example, but…) and your Factory is looking more like a maze. If OSHA were to see the plumbing in your factory…
Here’s a potential solution. The Builder model. You can create configuration methods that implement your variations and use only the level of detail you need to construct your test user. You can then do: UserBuilder.createUser() and get a default user, or be very specific: UserBuilder.createUser().withMemberLogin().withPremiumMembership().withCreditCardAutomaticBilling().withBlueEyes(), etc.