Skip to content

Randomization parameters

Mahmoud Ben Hassine edited this page Jun 11, 2016 · 21 revisions

The EnhancedRandomBuilder is the main entry point to set all parameters of how Random Beans generate values:

EnhancedRandom random = EnhancedRandomBuilder.aNewEnhancedRandomBuilder()
   .maxCollectionSize(10)
   .maxStringLength(20)
   .charset(forName("UTF-8"))
   .dateRange(today, tomorrow)
   .timeRange(nine, five)
   .scanClasspathForConcreteTypes(true)
   .seed(123L)
   .build();

Classpath Scanning

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 subtype 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 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

Clone this wiki locally