Skip to content

classpath scanning

Mahmoud Ben Hassine edited this page Mar 14, 2019 · 5 revisions

Classpath Scanning

When the target object declares a field of an abstract or interface type, Easy Random will throw an ObjectGenerationException saying that it was unable to create a random instance of the abstract/interface type. The scanClasspathForConcreteTypes parameter tells Easy Random 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:

EasyRandomParameters parameters = new EasyRandomParameters().scanClasspathForConcreteTypes(true);
EasyRandom easyRandom = new EasyRandom(parameters);

Foo foo = easyRandom.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, Easy Random will throw an ObjectGenerationException