Skip to content

Key APIs

benas edited this page Dec 21, 2014 · 21 revisions

The key API of jPopulator is the Populator interface:

public interface Populator {

    /**
     * Populate a java bean instance for the given type.<br/>
     * It is possible to exclude some fields from being populated using the excludeFieldsName param.
     *
     * @param type the type for which a java bean instance will be populated
     * @param excludedFields the name of fields to exclude
     * @param <T> the actual type of the target java bean
     * @return a populated instance of the given type
     */
    <T> T populateBean(final Class<T> type, final String... excludedFields);

    /**
     * Populate a random number of java bean instances for the given type.<br/>
     * It is possible to exclude some fields from being populated using the excludeFieldsName param.
     *
     * @param type the type for which java bean instances will be populated
     * @param excludedFields the name of fields to exclude
     * @param <T> the actual type of the target java bean
     * @return a list of populated instances of the given type
     */
    <T> List<T> populateBeans(final Class<T> type, final String... excludedFields);

    /**
     * Populate a fixed number of java bean instances for the given type.
     * It is possible to exclude some fields from being populated using the excludeFieldsName param.
     *
     * @param type the type for which java bean instances will be populated
     * @param excludedFields the name of fields to exclude
     * @param size the number of instances to populate
     * @param <T> the actual type of the target java bean
     * @return a list of populated instances of the given type
     */
    <T> List<T> populateBeans(final Class<T> type, final int size, final String... excludedFields);

}

You can populate a single instance or a random or fixed number of instances of any Java type. jPopulator will introspect your object type hierarchy, generate an instance for each nested bean and populate it with random data. You can also exclude some fields from being populated.

jPopulator comes with a default implementation of the Populator interface that you can create using the PopulatorBuilder API :

Populator populator = new PopulatorBuilder().build();

Note : The type of the object to populate must provide a default constructor and a public setter for each field (so that jPopulator can instantiate it and set random values to its fields). If your object is a standard java bean, default constructor and setters would be already defined in your object.