title: Does a framework like Factory Girl exist for Java?
body: <p>
  Factory Girl is a handy framework in rails for easily creating instances of models for testing.</p>
  
  <p>From the <a href="http://www.thoughtbot.com/projects/factory_girl/" rel="noreferrer"> Factory Girl home page</a>:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>An example (also from the home page):</p>
  
  <pre class="lang-rb prettyprint-override"><code>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
  </code></pre>
  
  <p>if I need a user a can just call</p>
  
  <pre class="lang-rb prettyprint-override"><code>test_user = Factory(:user, :admin =&gt; true)
  </code></pre>
  
  <p>which will yield a user with all the properties specified in the factory prototype, <em>except</em> 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.</p>
  
  <p>I'm thinking it should be pretty easy to implement something similar for Java, but I don't want to reinvent the wheel.</p>
  
  <p>P.S: I know about both JMock and EasyMoc, however I am not talking about a mocking framework here.</p>