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 Summary
Modifier and TypeMethodDescriptionasResult()
Returns aResult
containing the created object and seed value used to generate its values.assign
(Assignment... assignments) create()
Creates a new instance of a class and populates it with data.<V> InstancioApi
<T> generate
(TargetSelector selector, GeneratorSpec<V> spec) <V> InstancioApi
<T> generate
(TargetSelector selector, GeneratorSpecProvider<V> gen) ignore
(TargetSelector selector) lenient()
<V> InstancioApi
<T> onComplete
(TargetSelector selector, OnCompleteCallback<V> callback) <V> InstancioApi
<T> set
(TargetSelector selector, V value) stream()
Creates an infinite stream of distinct, fully populated object instances.subtype
(TargetSelector selector, Class<?> subtype) <V> InstancioApi
<T> supply
(TargetSelector selector, Supplier<V> supplier) <V> InstancioApi
<T> supply
(TargetSelector selector, Generator<V> generator) toModel()
Creates a model containing all the information for populating a class.verbose()
withMaxDepth
(int maxDepth) withNullable
(TargetSelector selector) withSeed
(long seed) withSettings
(Settings settings)
-
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
Returns aResult
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
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
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
- Since:
- 1.0.1
-
withNullable
- Since:
- 1.0.1
-
set
- Since:
- 1.2.3
-
supply
- Since:
- 1.0.1
-
supply
- Since:
- 1.0.1
-
generate
- Since:
- 2.2.0
-
generate
- Since:
- 2.6.0
-
onComplete
- Since:
- 1.0.4
-
subtype
- Since:
- 1.4.0
-
assign
- Since:
- 3.0.0
-
withMaxDepth
- Since:
- 2.9.0
-
withSettings
- Since:
- 1.0.1
-
withSeed
- Since:
- 1.0.1
-
lenient
InstancioApi<T> lenient()- Since:
- 1.4.1
-
verbose
- Since:
- 3.0.0
-