百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
I

instancio

> 测试质量
开源

一个库,用于为您的单元测试创建完全填充的对象。

1.2K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个库,用于为您的单元测试创建完全填充的对象。


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 persons = Instancio.ofList(Person.class).size(10).create();
  1. Create streams of objects:
Stream persons = Instancio.stream(Person.class).limit(5);
  1. Create generic types:
Pair, List> pairOfLists = Instancio.create(new TypeToken, List>>() {});
  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 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")
    .create();

Person marge = Instancio.of(simpsons)
    .set(field(Person::getFirstName), "Marge")
    .create();
  1. Use data feeds for data defined in external sources (e.g. CSV or JSON):
List person = Instancio.ofList(Person.class)
    .size(10)
    .applyFeed(all(Person.class), feed -> feed.ofResource("persons.csv"))
    .create();
  1. Define custom feeds for reuse across tests:
…
  1. Inject arguments into fields and parameters with JUnit (video tutorial):
@ExtendWith(InstancioExtension.class)
class ExampleTest {

    @Given
    private List randomList;

    @Test
    void example1(@Given String randomSting, @Given(SomeCustomProvider.class) customString) {
        //...
    }

    @ValueSource(strings = {"foo", "bar"})
    @ParameterizedTest
    void example2(String fooOrBar, @Given long randomLong) {
        // ...
    }
}
  1. Run tests against many samples of generated inputs:
@ExtendWith(InstancioExtension.class)
class ExampleTest {

    @RepeatedTest(100)
    void example(UUID randomUuid, Foo randomFoo, @Given(BarProvider.class) Bar customBar) {
        // runs the test method 100 times against generated inputs
    }
}

Main Features

  • Fully reproducible data in case of test failures.
  • Support for generics, record and sealed classes, Java 21 sequenced collections.
  • Data generation based on JPA and Bean Validation annotations (Hibernate, jakarta).
  • Flexible configuration options and SPIs.
  • Support for defining custom generators.
  • InstancioExtension for Junit @ExtendWith.
  • Object population via fields and setters.

Getting Started

  • User guide (instancio.org)

  • Javadocs (javadoc.io)

  • Quickstart (github.com) sample Maven project with usage examples

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

Maven Coordinates

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


    org.instancio
    instancio-junit
    RELEASE
    test

To use Instancio standalone, with TestNG, or other test frameworks, use instancio-core:


    org.instancio
    instancio-core
    RELEASE
    test

To better manage the different dependencies use instancio-bom:


    
        
            org.instancio
            instancio-bom
            RELEASE
            pom
            import
        
    

[!CAUTION] The org.instancio:instancio artifact on Maven central is an older dependency that should no longer be used.

Feedback

Feedback and bug reports are greatly appreciated!

  • Please submit an issue for bug reports and feature requests.
  • For general feedback or questions, please create a post in the Discussions.

Please check the User Guide, previous issues and discussions before creating a new one.

Sponsors

If you like Instancio, please consider supporting its maintenance and development by becoming a sponsor.

A big thank you to the current project sponsors:

and individual backers:

  • @mevlana14

Thanks to JetBrains for supporting this project with their open source licenses.

For Enterprise

Available as part of the Tidelift Subscription

The maintainers of Instancio and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Javadata-generatorjavajunitjunit-jupiter

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类测试质量
定价开源

> 相关工具

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.