[go: up one dir, main page]

Skip to content

A library that creates fully populated objects for your unit tests.

License

Notifications You must be signed in to change notification settings

patrickdronk/instancio

 
 

Repository files navigation

Instancio Maven Central License Quality Gate Status Coverage


What is it?

Instancio is a Java library that automatically creates and populates objects for your unit tests.

Instead of manually setting up test data:

Address address  = new Address();
address.setStreet("street");
address.setCity("city");
//...

Person person = new Person();
person.setFirstName("first-name");
person.setLastName("last-name");
person.setAge(22);
person.setGender(Gender.MALE);
person.setAddress(address);
//...

You can simply do the following:

Person person = Instancio.create(Person.class);

This one-liner returns a fully-populated person, including nested objects and collections.

The object is populated with random data that can be reproduced in case of test failure.

What else can Instancio do?

  1. Create collections of objects:
List<Person> persons = Instancio.ofList(Person.class).size(10).create();
  1. Create streams of objects:
Stream<Person> persons = Instancio.stream(Person.class);
  1. Create generic types:
Pair<List<Foo>, List<Bar>> pairOfLists = Instancio.create(new TypeToken<Pair<List<Foo>, List<Bar>>>() {});
  1. Customise generated values:
Person person = Instancio.of(Person.class)
    .generate(field(Person::getDateOfBirth), gen -> gen.temporal().localDate().past())
    .generate(field(Phone::getAreaCode), gen -> gen.oneOf("604", "778"))
    .generate(field(Phone::getNumber), gen -> gen.text().pattern("#d#d#d-#d#d-#d#d"))
    .subtype(all(AbstractAddress.class), AddressImpl.class)
    .supply(all(LocalDateTime.class), () -> LocalDateTime.now())
    .onComplete(all(Person.class), (Person p) -> p.setName(p.getGender() == Gender.MALE ? "John" : "Jane"))
    .create();
  1. Create reusable templates (Models) of objects:
Model<Person> simpsons = Instancio.of(Person.class)
    .set(field(Person::getLastName), "Simpson")
    .set(field(Address::getCity), "Springfield")
    .generate(field(Person::getAge), gen -> gen.ints().range(40, 50))
    .toModel();

Person homer = Instancio.of(simpsons)
    .set(field(Person::getFirstName), "Homer")
    .set(all(Gender.class), Gender.MALE)
    .create();

Person marge = Instancio.of(simpsons)
    .set(field(Person::getFirstName), "Marge")
    .set(all(Gender.class), Gender.FEMALE)
    .create();

Main Features

  • Fully reproducible data in case of test failures.
  • Support for generics, record and sealed classes.
  • Support for defining custom generators.
  • Support for generating data based on Bean Validation annotations.
  • Flexible configuration options.
  • InstancioExtension for Junit 5 @ExtendWith.
  • Support for Guava via instancio-guava module (experimental)

Documentation

Quickstart

Instancio Quickstart is the best way to get started. It is a sample (Maven) project that provides an overview of all the main features.

git clone https://github.com/instancio/instancio-quickstart.git

Latest Release

Version 3.1.0 is now available. A summary of new features is available in the release notes.

Maven coordinates

If you have JUnit 5 on the classpath, use the instancio-junit dependency.

<dependency>
    <groupId>org.instancio</groupId>
    <artifactId>instancio-junit</artifactId>
    <version>3.1.0</version>
    <scope>test</scope>
</dependency>

To use Instancio with JUnit 4, TestNG, or standalone, use instancio-core:

<dependency>
    <groupId>org.instancio</groupId>
    <artifactId>instancio-core</artifactId>
    <version>3.1.0</version>
    <scope>test</scope>
</dependency>

Feedback

Feedback and bug reports are greatly appreciated. Please submit an issue to report a bug, or if you have a question or a suggestion.

Extensions

instancio-jpa is an extension created by Moritz Becker that can be used for integration tests.

Instancio-jpa is an extension on top of the Instancio library that enables the creation and population of JPA entities including the subsequent persistence of these entities using JPA for the purpose of test data generation in integration tests. The extension uses the JPA metamodel to yield a persistable object graph. It further provides JPA based utilities that allows for straight forward persistence of the object graph.

Special thanks to

JetBrains and YourKit for supporting this project.

JetBrains logo

YourKit logo

About

A library that creates fully populated objects for your unit tests.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 99.4%
  • Other 0.6%