Wiki

Clone wiki

bean-randomizer / Home

About

Tool for generation of random beans. Will be useful for unit testing when you need just to fill objects with some data, without care much about particular values. Has a rich and flexible programmatic configuration (without annotations) with the ability to extend.

Usage

Add it as maven dependency:

<dependency>
  <groupId>org.bitbucket.brunneng.br</groupId>
  <artifactId>bean-randomizer</artifactId>
  <version>1.1.1</version>
</dependency>

Let's generate a bean of class Person with 2 properties: name and age.

RandomObjectGeneratorr r = new RandomObjectGenerator();
Person person = r.generateRandomObject(Person.class);
System.out.println("name: " + person.getName());
System.out.println("age: " + person.getAge());
prints:
name: XXXXXX7197
age: 382
But since it's random, these values can be different.

Features

  • Generation of nested beans, arrays, lists, sets and maps.

  • Built-in support for randomizing of classes:

    • String
    • Boolean
    • Enums, EnumSet
    • Integer, int
    • Long, long
    • Byte, byte
    • Short, short
    • Character, char
    • Double, double
    • Float, float
    • BigDecimal, BigInteger
    • UUID
    • Date, java.sql.Date, Timestamp, Instant, Calendar, GregorianCalendar, LocalDateTime, ZonedDateTime, OffsetDateTime
    • ZoneId
    • DayOfWeek
    • Locale
  • Ability to skip properties if you don't want to generate them.
  • Ability to add custom randomizers for specific types and for particular properties.
  • Supports applying the stable random seed per test (done by analyzing the current stack trace). This means that multiple launches of the test will result in the creation of the same sequences of random objects. This minimises the influence of random factors on tests and has such advantages as:

    1. Tests will be more stable.

    2. It will be easier to debug a problem in case the test is failed.

  • Full support of generic classes.

  • Defence against infinite recursion during the generation in case if same beans are nested.
  • Ability to automatically find in classpath the single implementation of the interface or abstract class, in order to instantiate an object.
  • Ability to create objects by constructor with arguments, if a no-arg constructor is not defined.
  • Inheritance of configurations. For example, you can configure base class, and this configuration will be applied to subclasses.

Configuration

Configuration c = new Configuration(); // To access global configurations.
Configuration.Bean bean = c.beanOfClass(Person.class); // To access bean-level configurations
Configuration.Bean.Property p = bean.property("name"); // To access property-level configurations
...
RandomObjectGenerator r = new RandomObjectGenerator(c); // applying configuration to randomizer

Configurations in Configuration

  • countOfDifferentValues - global count of different values generated by randomizers. By default is 10000.
  • minValue - Global min value, which can be generated by randomizers. By default is 0.0. If it doesn't match the type of property, then the property random generator should apply its own defaults
  • maxValue - Global max value (exclusive), generated by randomizers. By default is the double value of Integer.MAX_VALUE. If it doesn't match the type of property, then the property random generator should apply its own defaults.
  • maxBeanRecursionDepth - Max depth of recursion when bean has the bean of the same type as property (possibly indirect). Need to avoid stack overflow exception during generation. By default, it's 2.
  • sizeOfCollections - Global count of objects generated in collections and maps. By default, it's 1.
  • stringsLength - Global length of all generated strings. By default, it's 10.
  • timeZone - Id of time zone which will be globally applied to all newly generated dates (if it's supported by date class). By default UTC.
  • truncateTimeTo - Time unit to which all generated dates and local times should be truncated if the truncated value is not less than the min value. By default TimeTruncationUnit.MILLIS.
  • scanPackageToFindImplementation - Should the generator perform scanning of the package of an interface or abstract class to find implementation which can be instantiated? By default 'true'
  • flatMode - If set to 'true' then don't perform randomization of collections and associated beans. By default 'false'
  • method addRandomizer - Adds custom randomizer which has higher priority than basic ones.
  • method addDefaultImplementation - Adds default implementation of given interface or abstract class.
  • method addSkippedClass - Adds the skipped class. For it and all its subclasses, the random value generation will be skipped, i.e. they will be null.

Configurations in Configuration.Bean

  • countOfDifferentValues - Bean specific count of different values generated by randomizers.
  • minValue - Bean specific min value, which can be generated by randomizers.
  • maxValue - Bean specific max value, which can be generated by randomizers.
  • sizeOfCollection - Bean specific count of objects generated in collections and maps.
  • flatMode - If set to 'true' then for given bean don't perform randomization of collections and associated beans.

Configurations in Configuration.Bean.Property

  • countOfDifferentValues - Property specific count of different values generated by randomizers.
  • minValue - Property specific min value, which can be generated by randomizers.
  • maxValue - Property specific max value, which can be generated by randomizers.
  • sizeOfCollection - Property specific count of objects generated in collections and maps.
  • stringLength - Property specific length of generated string values.
  • skipped - Should this property be skipped during random generation?
  • customRandomizer - Custome randomizer of this property or null, if not specified. Supports inheritance of custom randomizer from parent property.

Updated