Skip to content

Key APIs

Mahmoud Ben Hassine edited this page Feb 19, 2016 · 21 revisions

The EnhancedRandom interface

The key API of Random Beans is the EnhancedRandom interface:

public abstract class EnhancedRandom extends Random {

    /**
     * Generate a random instance of the given type.
     *
     * @param type           the type for which an instance will be generated
     * @param excludedFields the name of fields to exclude
     * @param <T>            the actual type of the target object
     * @return a random instance of the given type
     * @throws ObjectGenerationException when unable to populate an instance of the given type
     */
    public abstract <T> T nextObject(final Class<T> type, final String... excludedFields);

}

You can generate a random instance any Java type using the nextObject() method. Random Beans will introspect your object type hierarchy, generate an instance for each nested bean and populate it with random data.

Random Beans comes with a default implementation of the EnhancedRandom interface that you can create using the EnhancedRandomBuilder API :

EnhancedRandom enhancedRandom = new EnhancedRandomBuilder().build();