-
Notifications
You must be signed in to change notification settings - Fork 236
Key APIs
Mahmoud Ben Hassine edited this page Jun 11, 2016
·
21 revisions
The key API of Random Beans is the EnhancedRandom
abstract class:
/**
* Abstract class for random object generator.
*
* @author Mahmoud Ben Hassine ([email protected])
*/
public abstract class EnhancedRandom extends Random {
/**
* Generate a random instance of the given type.
*
* @param type the target class type
* @param excludedFields the name of fields to exclude
* @param <T> the target type
* @return a random instance of the given type
*/
public static <T> T random(final Class<T> type, final String... excludedFields) {
return aNewEnhancedRandomBuilder().build().nextObject(type, excludedFields);
}
/**
* Generate a stream of random instances of the given type.
*
* @param amount the number of instances to generate
* @param type the type for which instances will be generated
* @param excludedFields the name of fields to exclude
* @param <T> the actual type of the target objects
* @return a stream of random instances of the given type
* @throws ObjectGenerationException when unable to populate an instance of the given type
*/
public static <T> Stream<T> randomStreamOf(final int amount, final Class<T> type, final String... excludedFields) {
return aNewEnhancedRandomBuilder().build().objects(type, amount, excludedFields);
}
/**
* Generate a {@link List} of random instances of the given type.
*
* @param amount the number of instances to generate
* @param type the type for which instances will be generated
* @param excludedFields the name of fields to exclude
* @param <T> the actual type of the target objects
* @return a list of random instances of the given type
* @throws ObjectGenerationException when unable to populate an instance of the given type
*/
public static <T> List<T> randomListOf(final int amount, final Class<T> type, final String... excludedFields) {
return randomStreamOf(amount, type, excludedFields).collect(toList());
}
/**
* Generate a {@link Set} of random instances of the given type.
*
* @param amount the number of instances to generate
* @param type the type for which instances will be generated
* @param excludedFields the name of fields to exclude
* @param <T> the actual type of the target objects
* @return a set of random instances of the given type
* @throws ObjectGenerationException when unable to populate an instance of the given type
*/
public static <T> Set<T> randomSetOf(final int amount, final Class<T> type, final String... excludedFields) {
return randomStreamOf(amount, type, excludedFields).collect(toSet());
}
/**
* Generate a {@link Collection} of random instances of the given type.
*
* @param amount the number of instances to generate
* @param type the type for which instances will be generated
* @param excludedFields the name of fields to exclude
* @param <T> the actual type of the target objects
* @return a collection of random instances of the given type
* @throws ObjectGenerationException when unable to populate an instance of the given type
*/
public static <T> Collection<T> randomCollectionOf(final int amount, final Class<T> type, final String... excludedFields) {
return randomListOf(amount, type, excludedFields);
}
/**
* 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);
/**
* Generate a stream of random instances of the given type.
*
* @param type the type for which instances will be generated
* @param amount the number of instances to generate
* @param excludedFields the name of fields to exclude
* @param <T> the actual type of the target objects
* @return a stream of random instances of the given type
* @throws ObjectGenerationException when unable to populate an instance of the given type
*/
public abstract <T> Stream<T> objects(final Class<T> type, final int amount, final String... excludedFields);
}
You can generate a random instance any Java type using the nextObject(Class type)
method or the random(Class type)
static 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 = EnhancedRandomBuilder.aNewEnhancedRandomBuilder().build();
Easy Random is created by Mahmoud Ben Hassine with the help of some awesome contributors!