Package org.instancio

Interface InstancioApi<T>

Type Parameters:
T - type to create
All Known Subinterfaces:
InstancioOfClassApi<T>, InstancioOfCollectionApi<C>

public interface InstancioApi<T>
Instancio API for generating instances of a class populated with random data.
Since:
1.0.1
  • Method Details

    • create

      T create()
      Creates a new instance of a class and populates it with data.

      Example:

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

      The returned object will have all its fields populated with random data, including collection and array fields.

      Returns:
      a fully populated object
      Since:
      1.0.1
    • asResult

      Result<T> asResult()
      Returns a Result containing the created object and seed value used to generate its values. The seed value can be used to reproduce the same object again.
      Returns:
      result containing the created object
      Since:
      1.5.1
    • stream

      Stream<T> stream()
      Creates an infinite stream of distinct, fully populated object instances.

      Example:

      
       List<Person> persons = Instancio.of(Person.class)
           .generate(field(Person::getAge), gen -> gen.ints().range(18, 100))
           .stream()
           .limit(5)
           .collect(Collectors.toList());
       

      Warning: limit() must be called to avoid an infinite loop.

      All objects in the stream are independent of each other.

      Returns:
      an infinite stream of distinct object instances
      Since:
      1.1.9
    • toModel

      Model<T> toModel()
      Creates a model containing all the information for populating a class.

      The model can be useful when class population needs to be customised and the customisations need to be re-used in different parts of the code.

      Example:

      
       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();
       
      Returns:
      a model that can be used as a template for creating objects
      Since:
      1.0.1
    • ignore

      InstancioApi<T> ignore(TargetSelector selector)
      Since:
      1.0.1
    • withNullable

      InstancioApi<T> withNullable(TargetSelector selector)
      Since:
      1.0.1
    • set

      <V> InstancioApi<T> set(TargetSelector selector, V value)
      Since:
      1.2.3
    • supply

      <V> InstancioApi<T> supply(TargetSelector selector, Supplier<V> supplier)
      Since:
      1.0.1
    • supply

      <V> InstancioApi<T> supply(TargetSelector selector, Generator<V> generator)
      Since:
      1.0.1
    • generate

      <V> InstancioApi<T> generate(TargetSelector selector, GeneratorSpecProvider<V> gen)
      Since:
      2.2.0
    • generate

      @ExperimentalApi <V> InstancioApi<T> generate(TargetSelector selector, GeneratorSpec<V> spec)
      Since:
      2.6.0
    • onComplete

      <V> InstancioApi<T> onComplete(TargetSelector selector, OnCompleteCallback<V> callback)
      Since:
      1.0.4
    • subtype

      InstancioApi<T> subtype(TargetSelector selector, Class<?> subtype)
      Since:
      1.4.0
    • assign

      @ExperimentalApi InstancioApi<T> assign(Assignment... assignments)
      Since:
      3.0.0
    • withMaxDepth

      InstancioApi<T> withMaxDepth(int maxDepth)
      Since:
      2.9.0
    • withSettings

      InstancioApi<T> withSettings(Settings settings)
      Since:
      1.0.1
    • withSeed

      InstancioApi<T> withSeed(long seed)
      Since:
      1.0.1
    • lenient

      InstancioApi<T> lenient()
      Since:
      1.4.1
    • verbose

      Since:
      3.0.0