Coder Social home page Coder Social logo

qornanali / java-factory-bot Goto Github PK

View Code? Open in Web Editor NEW

This project forked from topicusoverheid/java-factory-bot

0.0 1.0 0.0 164 KB

Library for creating objects as test data, inspired by the popular FactoryBot for ruby

License: Apache License 2.0

Java 1.59% Groovy 98.41%

java-factory-bot's Introduction

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.

java-factory-bot's People

Contributors

dennisschroer avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.