Releases: instancio/instancio
4.7.0
What's Changed
New Features
New top-level APIs for creating blank objects
Instancio.createBlank(Class<T>)
Instancio.ofBlank(Class<T>)
setBlank(TargetSelector)
A blank object has the following properties:
null
value fields (strings, numbers, dates)- blank nested POJOs
- empty arrays and collections
For example:
Person person = Instancio.createBlank(Person.class);
// Output:
// Person[
// name=<null>,
// dateOfBirth=<null>,
// phoneNumbers=[] // empty List
// address=Address[street=<null>, city=<null>, country=<null>] // blank nested POJO
//]
Customising the object using the builder API:
Person person = Instancio.ofBlank(Person.class)
.generate(field(Person::getPhoneNumbers), gen -> gen.collection().size(2))
.set(field(Address::getCountry), "Canada")
.set(field(Phone::getCountryCode), "+1")
.create();
// Output:
// Person[
// name=<null>,
// dateOfBirth=<null>,
// phoneNumbers=[Phone[countryCode=+1,number=<null>], Phone[countryCode=+1,number=<null>],
// address=Address[street=<null>, city=<null>, country=Canada]
//]
String generator spec unicode()
method
Comment comment = Instancio.of(Comment.class)
.generate(field(Comment::getText), gen -> gen.string().unicode().length(30))
.create();
// Sample output:
// Comment[text="﹩檘𤤑옖廪🕺样𝠴🪘𫢋荡𩓋喇䶷蝦𱠕𘡹𑜸𢴛𤫡ϛ𱪽𐳙𦾆𢰫𭏉𫥻𬒊儲Ẃ"]
The API also supports restricting characters to one or more Unicode blocks:
Character.UnicodeBlock[] blocks = {
Character.UnicodeBlock.EMOTICONS,
Character.UnicodeBlock.CYRILLIC
};
Comment comment = Instancio.of(Comment.class)
.generate(field(Comment::getText), gen -> gen.string().length(10).unicode(blocks))
.create();
// Sample output:
// Comment[text="ф😬😅😟фҖӝ😲ЭӍ"]
In addition, Unicode strings can be generated using the Gen
class:
String unicodeString = Gen.string().unicode().length(50).get();
New setting Keys.STRING_TYPE
The new setting allows customising generated string types via Settings
:
Comment comment = Instancio.of(Comment.class)
.withSetting(Keys.STRING_TYPE, StringType.ALPHANUMERIC)
.create();
or globally using instancio.properties
:
string.type=ALPHANUMERIC
Support for generating numeric sequence strings
Numeric sequence strings can be generated using the numericSequence()
spec method or Keys.STRING_TYPE
setting:
record Phone(String countryCode, String number) {}
List<Phone> phones = Instancio.ofList(Phone.class)
.withSetting(Keys.STRING_TYPE, StringType.NUMERIC_SEQUENCE)
.create();
// Sample output:
// [Phone[countryCode=1,number=2],
// Phone[countryCode=3,number=4],
// Phone[countryCode=5,number=6]]
This setting can be combined with Keys.STRING_FIELD_PREFIX_ENABLED
to produce sequential strings that are prefixed with field name:
List<Phone> phones = Instancio.ofList(Phone.class)
.withSetting(Keys.STRING_FIELD_PREFIX_ENABLED, true)
.withSetting(Keys.STRING_TYPE, StringType.NUMERIC_SEQUENCE)
.create();
// Sample output:
// [Phone[countryCode=countryCode_1,number=number_2],
// Phone[countryCode=countryCode_3,number=number_4],
// Phone[countryCode=countryCode_5,number=number_6]]
- Perf: minor string generator improvement by @armandino in #1017
- String generator spec
unicode()
method +StringType
setting by @armandino in #1018 - New setting
StringType.NUMERIC_SEQUENCE
for generating sequential strings by @armandino in #1019 - Add
numericSequence()
to string generator spec by @armandino in #1020 - Minor refactoring: extract selector base class by @armandino in #1022
- New top-level APIs for creating blank objects by @armandino in #1021
- If a primitive target is set to null, set the value to the primitive's default by @armandino in #1027
- Renamed
withBlank()
tosetBlank()
by @armandino in #1028 - Bump version.kotlin from 1.9.24 to 2.0.0 by @dependabot in #1023
- Bump jakarta.validation:jakarta.validation-api from 3.0.2 to 3.1.0 by @dependabot in #1025
- Bump jakarta.persistence:jakarta.persistence-api from 3.0.0 to 3.2.0 by @dependabot in #1024
- Revert "Bump jakarta.persistence:jakarta.persistence-api from 3.0.0 to 3.2.0" by @armandino in #1029
- Bug fix: collection/map spec with subtype throws exception from
GeneratorProvider
SPI by @armandino in #1030
Full Changelog: instancio-parent-4.6.0...instancio-parent-4.7.0
4.6.0
What's Changed
New Features
New top-level API filter()
Thefilter()
method can be used to filter generated values using a predicate. If the predicate evaluates to false
for a given value, a new value will be generated. The new value will also be tested against the predicate, and so on.
Example 1: generate a list of even numbers
List<Integer> evenNumbers = Instancio.ofList(Integer.class)
.filter(allInts(), (Integer i) -> i % 2 == 0)
.create();
Example 2: generate a list of objects with unique string and numeric values
Set<?> generatedValues = new HashSet<>();
List<Person> persons = Instancio.ofList(Person.class)
.size(100)
.filter(all(allInts(), allLongs(), allStrings()), generatedValues::add)
.create();
Suport for temporal generator spec min()
and max()
LocalDate maxDate = LocalDate.now().minusYears(18);
Person adult = Instancio.of(Person.class)
.generate(field(Person::getDateOfBirth), gen -> gen.temporal().localDate().max(maxDate))
.create();
// Sample output:
// Person[dateOfBirth=1984-01-26]
- Bump org.apache.maven.plugins:maven-jar-plugin from 3.4.0 to 3.4.1 by @dependabot in #996
- Bump org.apache.maven.plugins:maven-gpg-plugin from 3.2.3 to 3.2.4 by @dependabot in #997
- Bump com.github.siom79.japicmp:japicmp-maven-plugin from 0.20.0 to 0.21.0 by @dependabot in #998
- Bump com.github.siom79.japicmp:japicmp-maven-plugin from 0.21.0 to 0.21.1 by @dependabot in #999
- Bump com.puppycrawl.tools:checkstyle from 10.15.0 to 10.16.0 by @dependabot in #1002
- Bump org.apache.maven.plugins:maven-deploy-plugin from 3.1.1 to 3.1.2 by @dependabot in #1003
- Suport for temporal generator spec
min()
andmax()
by @armandino in #1010 - Bump version.jooq from 3.19.7 to 3.19.8 by @dependabot in #1009
- Bump com.github.spotbugs:spotbugs-maven-plugin from 4.8.4.0 to 4.8.5.0 by @dependabot in #1011
- Bump version.kotlin from 1.9.23 to 1.9.24 by @dependabot in #1013
- Bump com.github.siom79.japicmp:japicmp-maven-plugin from 0.21.1 to 0.21.2 by @dependabot in #1012
- Bug fix:
setModel()
with primitive selectors by @armandino in #1014 - New top-level API
filter()
by @armandino in #1015 - Bump com.google.guava:guava from 33.1.0-jre to 33.2.0-jre by @dependabot in #1008
Full Changelog: instancio-parent-4.5.1...instancio-parent-4.6.0
4.5.1
What's Changed
- Bump nl.jqno.equalsverifier:equalsverifier-nodep from 3.15.8 to 3.16 by @dependabot in #978
- Bump org.apache.maven.plugins:maven-gpg-plugin from 3.2.1 to 3.2.2 by @dependabot in #979
- Bump org.ow2.asm:asm from 9.6 to 9.7 by @dependabot in #977
- Bump com.puppycrawl.tools:checkstyle from 10.14.2 to 10.15.0 by @dependabot in #981
- Bump org.jacoco:jacoco-maven-plugin from 0.8.11 to 0.8.12 by @dependabot in #982
- Bump nl.jqno.equalsverifier:equalsverifier-nodep from 3.16 to 3.16.1 by @dependabot in #983
- Bump version.jooq from 3.19.6 to 3.19.7 by @dependabot in #985
- Bump org.apache.maven.plugins:maven-source-plugin from 3.3.0 to 3.3.1 by @dependabot in #984
- Bump com.github.spotbugs:spotbugs-maven-plugin from 4.8.3.1 to 4.8.4.0 by @dependabot in #986
- Bump org.apache.maven.plugins:maven-gpg-plugin from 3.2.2 to 3.2.3 by @dependabot in #988
- Bump com.tngtech.archunit:archunit from 1.2.1 to 1.3.0 by @dependabot in #987
- Bump org.slf4j:slf4j-api from 2.0.12 to 2.0.13 by @dependabot in #990
- Bump org.apache.maven.plugins:maven-jar-plugin from 3.3.0 to 3.4.0 by @dependabot in #989
- Add
InstancioExtension
to feature tests where missing by @armandino in #991 - Bugfix: generating empty
javax.xml.namespace.QName
objects leads to NPE by @armandino in #993 - Fix generator caching by @armandino in #994
Full Changelog: instancio-parent-4.5.0...instancio-parent-4.5.1
4.5.0
What's Changed
New Features
- New SPI for processing custom annotations (#975)
Improvements
- misc performance improvements
- Bump version.jooq from 3.19.5 to 3.19.6 by @dependabot in #955
- Bump com.puppycrawl.tools:checkstyle from 10.14.0 to 10.14.1 by @dependabot in #956
- Refactored model context / selector maps by @armandino in #960
- Bump org.apache.maven.plugins:maven-gpg-plugin from 3.1.0 to 3.2.0 by @dependabot in #957
- Fix typo in user-guide.md by @nok3zy in #961
- Use sonar-fork-analysis action by @EvaristeGalois11 in #962
- Refactored internal classes related to generation by @armandino in #965
- Bump org.sonarsource.scanner.maven:sonar-maven-plugin from 3.10.0.2594 to 3.11.0.3922 by @dependabot in #958
- Perf: reuse
DatatypeFactory
when creatingXMLGregorianCalendar
by @armandino in #967 - Perf: cache generators by @armandino in #968
- Bump com.puppycrawl.tools:checkstyle from 10.14.1 to 10.14.2 by @dependabot in #966
- Perf: use no-op classes to avoid unnecessary work by @armandino in #971
- Bump com.google.guava:guava from 33.0.0-jre to 33.1.0-jre by @dependabot in #959
- Bump org.apache.maven.plugins:maven-compiler-plugin from 3.11.0 to 3.13.0 by @dependabot in #970
- Rerfactor/cleanup selector classes by @armandino in #973
- Bump org.projectlombok:lombok from 1.18.30 to 1.18.32 by @dependabot in #972
- SPI for processing custom annotations by @armandino in #975
- Bump org.apache.maven.plugins:maven-gpg-plugin from 3.2.0 to 3.2.1 by @dependabot in #969
New Contributors
Full Changelog: instancio-parent-4.4.0...instancio-parent-4.5.0
4.4.0
What's Changed
Potentially breaking change
Allow selectors to overwrite initialised values even if Keys.OVERWRITE_EXISTING_VALUES
is set to false
Previously, selectors could not overwrite initialised values if OVERWRITE_EXISTING_VALUES
was set to false
. This behaviour made the setting too restrictive. Starting from this release, selectors can overwrite initialised values.
For details, see: https://www.instancio.org/user-guide/#initialised-fields
New Features
New top-level API: setModel(TargetSelector, Model)
The new API promotes reuse of models. It allows applying a model instance to specific targets via a selector.
Example:
record Foo(String value) {}
record Container(Foo fooA, Foo fooB) {}
Model<Foo> fooModel = Instancio.of(Foo.class)
.set(field(Foo::value), "foo")
.toModel();
The model can be applied to a specific Foo
field declared by the Container
:
Container container = Instancio.of(Container.class)
.setModel(field(Container::fooA), fooModel)
.create();
// Sample output:
// Container[fooA=Foo[value="foo"], fooB=Foo[value="ANBQNR"]]
Coordinate generator for latitude and longitude
Location location = Instancio.of(Location.class)
.generate(field(Location::lat), gen -> gen.spatial().coordinate().lat())
.generate(field(Location::lon), gen -> gen.spatial().coordinate().lon())
.create();
// Sample output: Location[lat=-77.80297026108207, lon=-122.08201176512672]
Thanks to @StevenPG for the contribution!
Support for marking an individual selector as lenient()
Example example = Instancio.of(Example.class)
.set(fields().named("fieldThatMayNotExist").lenient(), "foo") // will not result in unused selector error
.create();
Other Improvements
- better seed reporting by
instancio-junit
- improved
verbose()
output
- Support for marking an individual selector as
lenient()
by @armandino in #932 - Selectors to have higher precedence than
Keys.OVERWRITE_EXISTING_VALUES
by @armandino in #909 - Bump nl.jqno.equalsverifier:equalsverifier-nodep from 3.15.6 to 3.15.7 by @dependabot in #934
- Add selector matches to
verbose()
output by @armandino in #936 - Tests for
java.util.concurrent.locks.*
by @armandino in #937 - Predicate selector builder minor refactoring by @armandino in #938
- Bump version.jooq from 3.19.4 to 3.19.5 by @dependabot in #939
- Additional seed tests + user guide update (#940) by @armandino in #942
- Bump com.puppycrawl.tools:checkstyle from 10.13.0 to 10.14.0 by @dependabot in #941
- Bump com.github.siom79.japicmp:japicmp-maven-plugin from 0.18.5 to 0.19.1 by @dependabot in #944
- Bump nl.jqno.equalsverifier:equalsverifier-nodep from 3.15.7 to 3.15.8 by @dependabot in #945
- Bug fix: assignment with predicate selector by @armandino in #946
- Seed reporting improvements (#940) by @armandino in #948
- Bump com.github.siom79.japicmp:japicmp-maven-plugin from 0.19.1 to 0.20.0 by @dependabot in #950
- Bump version.kotlin from 1.9.22 to 1.9.23 by @dependabot in #952
- Coordinate generator for latitude and longitude by @StevenPG in #953
- New top-level API:
setModel(TargetSelector, Model)
by @armandino in #943
New Contributors
Full Changelog: instancio-parent-4.3.2...instancio-parent-4.4.0
4.3.2
What's Changed
As of this release, internal classes will no longer be accessible on the module path. This will be a breaking change for client code that relies on org.instancio.internal.*
or @InternalApi
annotated classes.
Previous releases of Instancio supported Java moduies via Automatic-Module-Name
. This release adds module-info.java
files with exports
for public APIs only. Module names are unchanged.
Big thanks to @grubeninspekteur for this contribution!
- Refactor
Result
from class to interface by @armandino in #923 - Added JPMS module information by @grubeninspekteur in #921
- Javadoc: add custom stylesheet by @armandino in #928
- Bump com.github.siom79.japicmp:japicmp-maven-plugin from 0.18.4 to 0.18.5 by @dependabot in #922
- Convert
instancio-test-support
to a Java module by @armandino in #929 - Bump version.jooq from 3.19.3 to 3.19.4 by @dependabot in #927
- Bump org.apache.maven.plugins:maven-antrun-plugin from 3.0.0 to 3.1.0 by @dependabot in #926
- Move JPMS smoke tests from external repository to the main project (#925) by @armandino in #930
New Contributors
- @grubeninspekteur made their first contribution in #921
Full Changelog: instancio-parent-4.3.1...instancio-parent-4.3.2
4.3.1
What's Changed
- Add convenience method for overriding a setting key. by @armandino in #917
- Bug fix: setting 'max' size setting to zero results in negative 'min' value by @armandino in #918
- Bug fix: bean validation NPE when creating a collection by @armandino in #919
- Bump com.github.siom79.japicmp:japicmp-maven-plugin from 0.18.3 to 0.18.4 by @dependabot in #916
Full Changelog: instancio-parent-4.3.0...instancio-parent-4.3.1
4.3.0
What's Changed
New Features
Support for ElementType.TYPE_USE
bean validation annotations (#911)
class Pojo {
@Size(min = 3, max = 3)
private Map<@Email String, @Negative Integer> map;
// ...
}
Pojo pojo = Instancio.of(Pojo.class)
.withSettings(Settings.create().set(Keys.BEAN_VALIDATION_ENABLED, true))
.create();
// Sample output:
// Pojo(map={vo4ira@zv5.com=-5809899, arhis@ktt2ricfw.net=-74162228, dpy99t@hdezns.org=-186211504})
Limitation: nested annotations are not supported. In the following example, the @Negative
annotation will be ignored:
Map<@Email String, List<@Negative Integer>> map;
emit()
spec support for recycling items (#905)
class Outer {
List<Mid> midList;
}
class Mid {
String value;
List<Inner> innerList;
}
class Inner {
String value;
}
Outer outer = Instancio.of(Outer.class)
.generate(field(Outer::getMidList), gen -> gen.collection().size(2))
.generate(field(Mid::getValue), gen -> gen.emit().items("A", "B"))
.generate(field(Mid::getInnerList), gen -> gen.collection().size(3))
.generate(field(Inner::getValue), gen -> gen.emit().items("X", "Y", "Z").whenEmptyRecycle())
.create();
assertThat(outer.getMidList())
.hasSize(2)
.allSatisfy(r -> assertThat(r.getInnerList())
.hasSize(3)
.extracting(Inner::getValue)
.containsExactly("X", "Y", "Z"))
.extracting(Mid::getValue)
.containsExactly("A", "B");
emit()
support for recycling items (#905) by @armandino in #906- Removed usage of internal JUnit APIs from
instancio-quickcheck
(#908) by @armandino in #912 - Support for
ElementType.TYPE_USE
bean validation annotations (#911) by @armandino in #914
Full Changelog: instancio-parent-4.2.0...instancio-parent-4.3.0
4.2.0
What's Changed
New Features
Support for predicate scopes and predicate selector within(Scope...)
and toScope()
- creating a
Scope
from a predicate selector:Select.scope(PredicateSelector)
PredicateSelector.toScope()
- narrowing down predicate selectors using
within(Scope...)
Example:
record Foo(String value) {}
record Container(Foo one, Foo two, Foo three) {}
Scope scope = fields(f -> f.getType() == Foo.class && f.getName().startsWith("t")).toScope();
Container container = Instancio.of(Container.class)
.set(fields().ofType(String.class).within(scope), "hello!")
.create();
// Sample output:
// Container(one=Foo(value=PDILAMI), two=Foo(value=hello!), three=Foo(value=hello!))
Added support for truncatedTo()
to applicable java.time
specs
Instant
LocalTime
LocalDateTime
OffsetTime
OffsetDateTime
ZonedDateTime
Duration.truncatedTo()
requires Java 9 and is currently not supported.
Examples:
record Venue(LocalTime openingTime) {}
Venue venue = Instancio.of(Venue.class)
.generate(field(Venue::openingTime), gen -> gen.temporal().localTime().truncatedTo(ChronoUnit.HOURS))
.create();
// Sample output:
// Venue[openingTime=18:00]
Using Gen
:
List<Instant> list = Gen.temporal().instant()
.truncatedTo(ChronoUnit.HOURS)
.list(3);
// Sample output:
// 1990-06-10T16:00:00Z
// 2031-02-10T10:00:00Z
// 2037-12-12T06:00:00Z
- Add support for
truncatedTo()
to applicablejava.time
specs (#881) by @armandino in #894 - Bump org.assertj:assertj-bom from 3.25.2 to 3.25.3 by @dependabot in #898
- Bug fix: propagate Map/Collection generator errors conditionally (#900) by @armandino in #902
- Bump org.junit:junit-bom from 5.10.1 to 5.10.2 by @dependabot in #897
- Bump org.slf4j:slf4j-api from 2.0.11 to 2.0.12 by @dependabot in #901
- Support for predicate scopes and predicate selector
within(Scope...)
andtoScope()
by @armandino in #896
Full Changelog: instancio-parent-4.1.0...instancio-parent-4.2.0
4.1.0
What's Changed
New Features
Support predicate selector scopes
record Widget(String name, String type) {}
record WidgetHolder(Widget widgetA, Widget widgetB) {}
WidgetHolder person = Instancio.of(WidgetHolder.class)
.set(fields(f -> f.getType() == String.class).within(scope(WidgetHolder::widgetB)), "foo")
.create();
// Sample output:
// WidgetHolder[widgetA=Widget[name=DGGM, type=MVCNQICSHC], widgetB=Widget[name=foo, type=foo]]
Bug fixes
- Error returning array/collection specs from generator provider SPI (#882)
- Unconditional assignment with records (#892)
- Bump org.slf4j:slf4j-api from 2.0.10 to 2.0.11 by @dependabot in #876
- Bump nl.jqno.equalsverifier:equalsverifier-nodep from 3.15.5 to 3.15.6 by @dependabot in #878
- Bug fix: error returning array/collection specs from generator provider SPI by @armandino in #882
- Bump org.apache.maven.plugins:maven-failsafe-plugin from 3.2.3 to 3.2.5 by @dependabot in #879
- Bump org.apache.maven.plugins:maven-surefire-plugin from 3.2.3 to 3.2.5 by @dependabot in #877
- Collection/map generator cleanup + tests by @armandino in #883
- Predicate selectors: support for
within(Scope)
by @armandino in #885 - Bump version.jooq from 3.19.1 to 3.19.2 by @dependabot in #884
- Bump actions/cache from 3 to 4 by @dependabot in #887
- Bump version.jooq from 3.19.2 to 3.19.3 by @dependabot in #888
- Bump org.assertj:assertj-bom from 3.25.1 to 3.25.2 by @dependabot in #889
- Bump com.puppycrawl.tools:checkstyle from 10.12.7 to 10.13.0 by @dependabot in #891
- Bug fix: unconditional assignment with records by @armandino in #892
Full Changelog: instancio-parent-4.0.0...instancio-parent-4.1.0