shamus / blueprints

yet another factory library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A library for setting up Java objects as test data.

Define strategies for building test fixtures at runtime, within the context of your tests.

This library is inspired by factory_bot

Defining a blueprint

Given this simple Java object, which also has a builder method provided by Lombok.

@Builder
@Value
public class Person
{
    Long id;
    String name;
    Integer age;
}

A blueprint can be provided which specifies how to generate data for each of the object's properties. Each provider is assigned to a field in the blueprint whose name corresponds to that of the object being built;

This blueprint will generate Person objects with the name set to "Alice"

@Blueprint(Person.class)
public class PersonBlueprint
{
    @PropertyDefault
    public String name = "Alice";
}

This blueprint will generate Person objects with:

@Blueprint(Person.class)
public class PersonBlueprint
{
    @PropertyDefault
    public String name = "Alice";

    @PrpertyDefault
    public Supplier<String> age = () -> Year.now().getValue() - 1976;
}

This blueprint will generate Person objects with:

  • the name set to "Alice"
  • the age set to a value generated by a simple function
  • the id set to a unique value generated against a sequence
@Blueprint(Person.class)
public class PersonBlueprint
{
    @PropertyDefault
    public SequencedSupplier<String> id = (n) -> n;

    @PropertyDefault
    public String name = "Alice";

    @PrpertyDefault
    public Supplier<String> age = () -> Year.now().getValue() - 1976;
}

Loading blueprints and using Factories

Blueprint definitions are loaded at runtime by scanning the classpath for specific packages. The blueprint classes are then used to create factories which will build Java objects.

  Factories factories = new FactoryScanner("blueprints.models");

  // PersonConfiguration defines a DSL for overriding property defaults.
  // This class is automatically generated from a blueprint and in named
  // <ModelName>Configuration by convention.
  Factory<Person, PersonConfiguration> factory = factories.get(Person.class);
  Person person = factory.create();

If the defaults provided by a blueprint don't meet your needs, they can be overridden using a DSL that is generated from the blueprint:

Person person = factory.create((configuration) -> {
    configuration.name("Ada");
    configuration.age(36);
});

About

yet another factory library


Languages

Language:Java 100.0%