java-factory-bot

Library for creating objects as test data

License

License

Categories

Categories

Java Languages
GroupId

GroupId

nl.topicus.overheid
ArtifactId

ArtifactId

java-factory-bot
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

java-factory-bot
Library for creating objects as test data
Project URL

Project URL

https://github.com/topicusoverheid/java-factory-bot
Source Code Management

Source Code Management

https://github.com/topicusoverheid/java-factory-bot

Download java-factory-bot

How to add to project

<!-- https://jarcasting.com/artifacts/nl.topicus.overheid/java-factory-bot/ -->
<dependency>
    <groupId>nl.topicus.overheid</groupId>
    <artifactId>java-factory-bot</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/nl.topicus.overheid/java-factory-bot/
implementation 'nl.topicus.overheid:java-factory-bot:0.2.0'
// https://jarcasting.com/artifacts/nl.topicus.overheid/java-factory-bot/
implementation ("nl.topicus.overheid:java-factory-bot:0.2.0")
'nl.topicus.overheid:java-factory-bot:jar:0.2.0'
<dependency org="nl.topicus.overheid" name="java-factory-bot" rev="0.2.0">
  <artifact name="java-factory-bot" type="jar" />
</dependency>
@Grapes(
@Grab(group='nl.topicus.overheid', module='java-factory-bot', version='0.2.0')
)
libraryDependencies += "nl.topicus.overheid" % "java-factory-bot" % "0.2.0"
[nl.topicus.overheid/java-factory-bot "0.2.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.codehaus.groovy : groovy-all jar 2.4.0
com.github.javafaker : javafaker jar 0.14

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.10

test (1)

Group / Artifact Type Version
org.spockframework : spock-core jar 1.1-groovy-2.4

Project Modules

There are no modules declared in this project.

java-factory-bot

Build Status

A library for creating objects as test data, with support for persisting the objects in a database.

Using factories, creating default sane test objects is simple, while individual attibutes can easily be tweaked. Combining these with java-faker allows to boost your tests, or seed your database for demo and testing purposes.

This library is inspired by factory_bot, a popular gem for ruby.

Example

Given a model for an Article and a User (getters and setters are omitted):

@Data
public class Article {
    private String title;
    private String content;
    private Date creationDate;
    private User author;
}

@Data
public class User {
    private String username;
    private String firstName;
    private String lastName;
    private String email;
}

we can define factories like

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

class UserFactory extends Factory<User> {
    Map<String, Attribute> attributes = [
            username : value { faker.name().username() },
            firstName: value { faker.name().firstName() },
            lastName : value { faker.name().lastName() },
            email    : value { "${get("firstName")}.${get("lastName")}@example.com" }
    ]
}

and create objects using

Article article = new ArticleFactory().build()

which generates an article with default random but sane attributes. Individual attributes or relations can be overriden by passing them in a map:

Article article = new ArticleFactory().build([title: "Foo", user: [username: "johndoe"]])

For documentation and more examples, visit the wiki.

Installation

Maven

Add the following to your dependencies:

<dependency>
    <groupId>nl.topicus.overheid</groupId>
    <artifactId>java-factory-bot</artifactId>
    <version>0.2.0</version>
    <scope>test</scope>
</dependency>

Gradle

Add the following line to the dependency section of build.gradle

compile "nl.topicus.overheid:java-factory-bot:0.2.0"

Building

Execute ./mvnw install to build and test the library.

Source and javadoc

To generate jars containing the source and javadoc, enable the source profile. Example:

./mvnw install -P source

Deploy

To deploy the library to maven central, enable the release and source profiles and perform the deploy goal:

./mvnw clean install deploy -P source -P release

Licence

This library is released under the Apache 2.0 licence, which you can find here.

nl.topicus.overheid

Topicus Sociaal Domein

Gids in het sociaal domein

Versions

Version
0.2.0
0.1.0