Skip to content

classpath scanning

Mahmoud Ben Hassine edited this page Feb 22, 2016 · 5 revisions

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