Skip to content
Dennis edited this page Apr 11, 2018 · 2 revisions

Welcome to the java-factory-bot wiki!

This wiki is still under development

Factories

Defining factories

Factories are based around a map of attributes, specifying the default values of attributes in the generated object.

class ArticleFactory extends Factory<Article> {
    Map<String, Attribute> attributes = [
            title       : attribute { faker.lorem().sentence() },
            content     : attribute { faker.lorem().paragraph() },
            creationDate: attribute { faker.date().past(20, TimeUnit.DAYS) },
            summary     : attribute { null },
            author      : hasOne(UserFactory),
            comments    : hasMany(CommentFactory)
    ]
}

Using factories

Each factory defines methods for building and creating objects. Examples:

// Generates an unsaved Article instance
Article article = new ArticleFactory().build()

// Generates a saved Article instance
Article article = new ArticleFactory().create()

// Generates a map containing generated attribute values
Map<String, Object> values = new ArticleFactory().buildAttributes()

// Each of these methods supports overriding values by passing them as arguments
// Generates an unsaved Article instance with title "Factories are cool"
Article article = new ArticleFactory().build([title: "Factories are cool"])

// Equal to previous line
Article article = new ArticleFactory().build(title: "Factories are cool")

// Nested overrides are supported
// Generates a saved article with a saved author with first name "Dennis"
Article article = new ArticleFactory().create([author: [firstName: "Dennis"]])

Note: In the examples groovy is used as language, because this language is suited for writing non-verbose code. However, java and other JVM languages can be used as well.

Attributes

The basis of a factory consists of a map of attributes. Each key in the map is the name of an attribute in the generated object, the value is an Attribute instance describing how the value of the attribute is determined.

The map of attributes is initialized by overriding the getAttributes() method. In groovy, a simple definition of a variable called attributes is sufficient. In the examples in this wiki, groovy is used as base language. However, multiple languages can be used to define and use factories.

To aid the creation of attributes, several helper methods are defined.

Fixed attributes

To set the value of an attribute to a fixed value, use the attribute helper:

class ArticleFactory extends Factory<Article> {
    Map<String, Attribute> attributes = [
        title: attribute { "Hey there" }
    ]
}

The attribute method is takes a closure which should, when executed, return the desired value of the attribute.

Calculated attributes

Instead of returning a fixed value, the closure can also return a calculated attribute.

class ArticleFactory extends Factory<Article> {
    Map<String, Attribute> attributes = [
        creationDate: attribute { new Date() - 20 }
    ]
}

Dependent attributes

Relations

Faker

Hooks

Traits

Configuration

Quick note here for using hibernate:

FactoryManager.instance.createContext = new FactoryContext(){
    @Override
    public <M> M perist(M object){
        session.save(object);
        return object;
    }
}

Factories in java

Factories in kotlin