Skip to content

Supported types

Mahmoud Ben Hassine edited this page Nov 8, 2020 · 18 revisions

Supported types

1. Java types

By default, Easy Random can generate random values for all Java built-in types (primitive and boxed) : int, long, String, Boolean, etc

2. Enumeration types

Enumeration types are also supported. Easy Random will pick up a random value from the enumeration's values and set it to the field of the enumeration type.

3. Arrays and Collection types

Easy Random can handle arrays and collection types defined in the Java Collections Framework.

You can also use a custom randomizer to populate a collection with random elements. Let's see an example:

public class Transaction {
   private Set<Account> accounts;
}

You can create a custom AccountRandomizer and use it as a delegate with the built-in SetRandomizer:

Randomizer<Account> accountRandomizer = new Randomizer<Account>() {

   private EasyRandom easyRandom = new EasyRandom();

   @Override
   public Account getRandomValue() {
      return easyRandom.nextObject(Account.class);
   }

};

EasyRandomParameters parameters = new EasyRandomParameters()
       .randomize(FieldPredicates.named("accounts")and(FieldPredicates.ofType(Set.class)).and(FieldPredicates.inClass(Transaction.class)), new SetRandomizer<Account>(accountRandomizer));
EasyRandom easyRandom = new EasyRandom(parameters);

Transaction transaction = easyRandom.nextObject(Transaction.class);

In this example, we use the SetRandomizer that generates a random number of accounts using the AccountRandomizer.

Easy Random comes with several randomizers for collections types defined in the Java Collection Framework:

  • org.jeasy.random.randomizers.collection.ListRandomizer
  • org.jeasy.random.randomizers.collection.SetRandomizer
  • org.jeasy.random.randomizers.collection.QueueRandomizer
  • org.jeasy.random.randomizers.collection.MapRandomizer

You can use these collection randomizers with a delegate randomizer in which you define how to generate random elements in the collection.

4. Date and Time types

Easy Random supports date types defined in the java.util package. As of version 3.0, it added support for java.time (JSR 310) types as well. However, javax.xml.datatype types (namely Duration and XMLGregorianCalendar) are not supported, you can use a custom randomizers for these types.