Java Testing Library

Instancio

A Java library for automating test data setup. Creates fully-populated objects with random data so you can spend less time writing boilerplate and more time writing tests.

// Inject a random Person using @Given
@ExtendWith(InstancioExtension.class)
class PersonMapperTest {

    @Test
    void shouldMapToDto(@Given Person person) {
        PersonDto dto = PersonMapper.toDto(person);
        assertThat(dto.getName()).isEqualTo(person.getName());
        assertThat(dto.getEmail()).isEqualTo(person.getEmail());
    }
}
// Or create manually with overrides
Person person = Instancio.of(Person.class)
    .set(field(Person::getName), "Alice")
    .generate(field(Person::getAge),
        gen -> gen.ints().range(18, 65))
    .create();
Latest release v5.5.1 @instancio_java

Instancio is free and open-source. If it's saved you time writing boilerplate, please consider sponsoring the project — it helps keep development going. A huge thank you to everyone who already has.

Become a supporter

How Instancio works

Object graph population

Uses reflection to populate every field in a class, including nested objects, generics, and collections. A single method call returns a fully-initialised instance ready for use in a test.

Randomised inputs

Each test run uses a different set of values. This can surface edge cases and bugs that wouldn't be caught with a fixed dataset. A seed is printed on failure so runs are reproducible.

Selective overrides

Override specific fields using set(), generate(), or supply(). Fields that aren't relevant to a test can be ignored. Everything else is handled automatically.