I also looked for a Java equivalent of Factory Girl, but never found anything like it. Instead, I created a solution from scratch. A factory for generating models in Java: Model Citizen.
Inspired by Factory Girl, it uses field annotations to set defaults for a Model, a simple example from the wiki:
@Blueprint(Car.class)
public class CarBlueprint {
@Default
String make = "car make";
@Default
String manufacturer = "car manufacturer";
@Default
Integer mileage = 100;
@Default
Map status = new HashMap();
}
This would be the Blueprint for the Car model. This is registered into the ModelFactory, than new instances can be created as follows:
ModelFactory modelFactory = new ModelFactory();
modelFactory.registerBlueprint( CarBlueprint.class );
Car car = modelFactory.createModel(Car.class);
You can override the values of the Car model by passing in an instance of Car instead of the Class and setting values as needed:
Car car = new Car();
car.setMake( "mustang" );
car = modelFactory.createModel( car );
The wiki has more complex examples (such as using @Mapped) and details for a few more bells and whistles.