-
Notifications
You must be signed in to change notification settings - Fork 236
Randomization parameters
The EnhancedRandomBuilder
is the main entry point to set all parameters of how to generate values:
EnhancedRandom random = EnhancedRandomBuilder.aNewEnhancedRandomBuilder()
.seed(123L)
.objectPoolSize(100)
.randomizationDepth(3)
.charset(forName("UTF-8"))
.timeRange(nine, five)
.dateRange(today, tomorrow)
.stringLengthRange(5, 50)
.collectionSizeRange(1, 10)
.scanClasspathForConcreteTypes(true)
.overrideDefaultInitialization(false)
.ignoreRandomizationErrors(true)
.build();
These parameters will be applied to all fields of the object graph.
The collectionSizeRange
parameter lets you bound the generated collections size.
Setting the stringLengthRange
parameter tells random beans to generate random strings with a bounded size.
You can use the charset
parameter to generate random values from the specified Charset
in all character-based fields, basically strings and characters. This parameter will be applied to all String
and Character
fields in the object graph.
You can use the dateRange
and timeRange
parameters to generate Date and Time values in a given range.
The EnhancedRandom
API can be configured with a seed
option in order to generate the same random instances.
Using the same seed, each run will produce the same value. This feature is useful when you want stable random values across JVM restarts (in tests for instances).
When the target object declares a field of an abstract or interface type, Random Beans will throw an ObjectGenerationException
saying that it was unable to create a random instance of the abstract/interface type. The scanClasspathForConcreteTypes
parameter tells Random Beans to scan the classpath and look for a concrete type of the abstract/interface field. Let's see a quick example:
abstract class Bar {}
class ConcreteBar extends Bar {}
class Foo {
private Bar bar;
}
Let's try to generate a random instance of type Foo
:
EnhancedRandom enhancedRandom = EnhancedRandomBuilder.aNewEnhancedRandomBuilder()
.scanClasspathForConcreteTypes(true)
.build();
Foo foo = enhancedRandom.nextObject(Foo.class);
In the generated Foo
instance, the bar
field will be assigned a random instance of the ConcreteBar
type.
Without setting the scanClasspathForConcreteTypes
parameter, Random Beans will throw an ObjectGenerationException
By default, Random Beans does not randomise fields that are already initialized:
public class Bean {
Set<String> strings = new HashSet<>();
List<Integer> integers = Arrays.asList(1, 2, 3);
public BeanWithDefaultValues() {
}
}
If you randomize an instance of this Bean
class, the strings
field will be empty, and the integers
fields will be equal to the list containing 1, 2 and 3. If you want to override this default initialization and randomize these fields, you need to set the overrideDefaultInitialization
parameter.
When a type declares a field of the same type, Random Beans will not be able to generate a random object graph due to infinite recursion. Here is an example:
class Person {
private String name;
private Person parent;
}
By default, Random Beans will generate at most 10 objects of type Person
. After that, it will reuse same objects from the pool for any field of type Person
. You can set the object pool size using the objectPoolSize
parameter.
The randomizationDepth
allows you to limit the randomization depth in an object graph. For example, if you have the following classes:
class A {
private B b;
}
class B {
private C c;
}
If you randomize an instance of type A
with randomizationDepth = 2
, the c
field in the generated B
instance will be null, that is, only the first 2 levels of the object graph are randomized.
By default, when Random Beans is not able to randomize a field or type, it will throw a ObjectGenerationException
. If you want to silently ignore randomization errors, you can set the ignoreRandomizationErrors
parameters. With this parameter set, any exception raised during the randomization
process will be silently ignored and the corresponding field will be set to null.
Easy Random is created by Mahmoud Ben Hassine with the help of some awesome contributors!