Class TestEntityBuilder

java.lang.Object
io.ebean.test.TestEntityBuilder

public class TestEntityBuilder extends Object
Builds entity bean instances with randomly populated scalar fields for use in tests.

@Id and @Version properties are left at their defaults (zero/null). @ManyToOne and @OneToOne relationships that have cascade persist are recursively built and set. Collection relationships (@OneToMany, @ManyToMany) are left empty — the caller can populate them if needed.

  TestEntityBuilder builder = TestEntityBuilder.builder(DB.getDefault()).build();

  // build in-memory (not saved to database)
  MyEntity entity = builder.build(MyEntity.class);

  // build and insert to the database
  MyEntity entity = builder.save(MyEntity.class);

  // supply a custom value generator for domain-specific values
  TestEntityBuilder builder = TestEntityBuilder.builder(DB.getDefault())
      .valueGenerator(myGenerator)
      .build();
  • Method Details

    • builder

      public static TestEntityBuilder.Builder builder(io.ebean.Database database)
      Returns a new TestEntityBuilder.Builder for the given database.
    • build

      public <T> T build(Class<T> beanClass)
      Build and return an instance of the entity class with scalar fields populated with random values. The entity is not saved to the database.
      Parameters:
      beanClass - the entity class to build
      Throws:
      IllegalArgumentException - if the class is not a known Ebean entity
    • save

      public <T> T save(Class<T> beanClass)
      Build an instance of the entity class, insert it to the database, and return it.
      Parameters:
      beanClass - the entity class to build and save
      Throws:
      IllegalArgumentException - if the class is not a known Ebean entity