Data Factory

Library for generating rich objects

License

License

Categories

Categories

Data
GroupId

GroupId

com.github.srang
ArtifactId

ArtifactId

data-factory
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Data Factory
Library for generating rich objects
Project URL

Project URL

https://github.com/srang/DataFactory
Source Code Management

Source Code Management

http://github.com/srang/DataFactory/tree/master

Download data-factory

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.srang/data-factory/ -->
<dependency>
    <groupId>com.github.srang</groupId>
    <artifactId>data-factory</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.srang/data-factory/
implementation 'com.github.srang:data-factory:1.0.1'
// https://jarcasting.com/artifacts/com.github.srang/data-factory/
implementation ("com.github.srang:data-factory:1.0.1")
'com.github.srang:data-factory:jar:1.0.1'
<dependency org="com.github.srang" name="data-factory" rev="1.0.1">
  <artifact name="data-factory" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.srang', module='data-factory', version='1.0.1')
)
libraryDependencies += "com.github.srang" % "data-factory" % "1.0.1"
[com.github.srang/data-factory "1.0.1"]

Dependencies

compile (3)

Group / Artifact Type Version
commons-lang : commons-lang jar 2.6
org.springframework : spring-core jar 4.3.4.RELEASE
com.github.javafaker : javafaker jar 0.10

test (3)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.10
junit : junit jar 4.12
org.hamcrest : java-hamcrest jar 2.0.0.0

Project Modules

There are no modules declared in this project.

data-factory

Test framework for generating rich objects with dummy data

Inspired and leverages java-faker

Usage

In your pom.xml, add the following:

<dependency>
  <groupId>com.github.srang</groupId>
  <artifactId>data-factory</artifactId>
  <version>1.0.3</version>
</dependency> 

In your Java code define a class you want to generate for example:

public class Fruit {
    public String name;
    public String description;

    public Fruit(String name, String description) {
        this.name = name;
        this.description = description;
    }
}

Then to start generating Fruit instances, you'll need to instantiate a DataFactory:

DataFactory<Fruit> fruitFactory = new BaseFactory<>(Fruit.class);

Then you can start generating objects:

// generate one fruit
Fruit fruit = fruitFactory.generate();

// or generate a list of fruits
List<Fruit> fruits = fruitFactory.generate(6);

Factory Customization

There are a number of ways to customize a DataFactory:

// inline field filter
fruitFilter.addFilter(
                (Field field) -> field.getType().equals(String.class) 
                   && field.getName().toLowerCase().contains("name"),
                () -> "Apple");

Fruit apple = fruitFactory.generate();
apple.getName(); // the name is "Apple"

// add custom language/locale
Locale bos =  new Locale("eng","boston"); // colloquialisms
DataFactory<Fruit> bostonFruitFactory = new BaseFactory<>(Fruit.class, bos);
bostonFruitFactory.generate().getName(); // wahtamelon

Nesting and Inheritance

A DataFactory can be used with complex objects that have List fields, nested objects, and/or inherit fields from parent objects. Going back to the Fruit example, let's create a couple supporting objects:

public class FruitBasket {
    public String material;
    public List<Fruit> contents;

    public Fruit(String material, List<Fruit> contents) {
        this.material = material;
        this.contents = contents;
    }
}

public class Berry extends Fruit {
    public Integer seedCount;
    
    public Berry(String name, String description, Integer seedCount) {
        super(name, description);
        this.seedCount = seedCount;
    }
}

Versions

Version
1.0.1
1.0.0