Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a framework like Factory Girl exist for Java? [closed]

Factory Girl is a handy framework in rails for easily creating instances of models for testing.

From the Factory Girl home page:

factory_girl allows you to quickly define prototypes for each of your models and ask for instances with properties that are important to the test at hand.

An example (also from the home page):

Factory.sequence :email do |n|
    "somebody#{n}@example.com"
end

# Let's define a factory for the User model. The class name is guessed from the
# factory name.
Factory.define :user do |f|
    # These properties are set statically, and are evaluated when the factory is
    # defined.
    f.first_name 'John'
    f.last_name  'Doe'
    f.admin      false
    # This property is set "lazily." The block will be called whenever an
    # instance is generated, and the return value of the block is used as the
    # value for the attribute.
    f.email      { Factory.next(:email) }
end

if I need a user a can just call

test_user = Factory(:user, :admin => true)

which will yield a user with all the properties specified in the factory prototype, except for the admin property which I have specified explicitly. Also note that the email factory method will yield a different email each time it is called.

I'm thinking it should be pretty easy to implement something similar for Java, but I don't want to reinvent the wheel.

P.S: I know about both JMock and EasyMoc, however I am not talking about a mocking framework here.

like image 943
KaptajnKold Avatar asked Jun 12 '09 12:06

KaptajnKold


3 Answers

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.

like image 166
mguymon Avatar answered Nov 18 '22 02:11

mguymon


One possible library for doing this is Usurper.

However, if you want to specify properties of the objects you are creating, then Java's static typing makes a framework pointless. You'd have to specify the property names as strings so that the framework could look up property accessors using reflection or Java Bean introspection. That would make refactoring much more difficult.

It's much simpler to just new up the objects and call their methods. If you want to avoid lots of boilerplate code in tests, the Test Data Builder pattern can help.

like image 45
Nat Avatar answered Nov 18 '22 02:11

Nat


  1. I understand this isn't for everybody, but you could write Ruby test code against your Java code. (JTestR)
  2. The preferred way of doing this in Java is using the Test Data Builder pattern. I would argue that this approach doesn't really warrant introducing the complexity of a framework or external dependency. I just don't see how you could specify much less using a framework and get anything more out of it... the Builder syntax is essentially equivalent to your FactoryGirl syntax. (Someone feel free to convince me otherwise!)
like image 2
cwash Avatar answered Nov 18 '22 03:11

cwash