Grails Fixtures plugin – object templates

Grails Fixtures plugin gives you a Grails domain object factory (or object mother if you like fancy names :)) thats lets you define your test fixture using a DSL.

Now, starting from the current snapshot (1.0.8-SNAPSHOT version) you can define domain object templates. A template defines an object with some common properties that will be replicated in all object instances based on that template.

Consider the following GORM domain class:

class Flight {
  String departure
  String arrival
  double price

You want to define a template for Warsaw – Szczecin flights having the same departure and arrival airport but different prices. You define the template adding a closure that marks the object as an abstract bean:

wawSzzFlight(Flight, departure: "WAW", arrival: "SZZ") { it.abstract = true }

Then you create concrete objects referencing the template through a closure that tells that the object definition should be based on the template:

wawSzzCheapFlight(Flight, price: 10.0) { it.parent = wawSzzFlight }
wawSzzExpensiveFlight(Flight, price: 300.0) { it.parent = wawSzzFlight }
// departure == "WAW" and arrival == "SZZ" of both cheap and expensive flights.

Note that you cannot reference the template object in your tests – BeanIsAbstractException will be raised.

Update you Fixtures plugin version and enjoy the new feature!