Skip to content

known limitations

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

Types with no default constructor

Easy Random is designed to generate random Java Beans. By definition, A Java Bean should provide a default constructor. If the target type does not provide a default constructor, Easy Random will still try to generate a random instance of it, but this is not guaranteed to work in all cases. In fact, Easy Random will fall back to objenesis for object creation (this can be overridden by providing a custom ObjectFactory instead of the default ObjenesisObjectFactory) which by-passes any constructor.. This results in a couple of un-expected side effects:

  • initialization code within constructors is not called
  • default initialization of fields is overridden even if overrideDefaultInitialization=false (See #228)

Type erasure

Easy Random uses the Java Reflection API to introspect object types at runtime and generate random data accordingly. Due to type erasure, there are some cases where Easy Random is not able to get the information required about a class or a field type at runtime. That said, it will still try to get the generic type information from the hierarchy. As of v4.3, Easy Random added support for common uses cases when a field is generic, like in the following example:

class A<T> {
   T t;
}
class B extends A<String>{}
class C extends C<Long>{}

In this example, Easy Random is able to generate random instances of B and C with the corresponding type of field t. However, it does not support complex types hierarchies with complex generic type definitions like class D extends A<Map<String, List<String>>. In these cases, you need to use a custom randomizer for those types.

Composite collections

Easy Random does not support composite collections like List<List<String>> or Map<String, List<Integer>>. This is technically possible (even though it's quite hard to imagine all possible combinations and come up with a generic solution) but obviously will make the implementation complex for no real added value. It is always possible to register a custom (collection) randomizer for such types.

Android compatibility

Easy Random is known to not play well with Android. This is due to how Dalvik VM differs from a regular JVM (some Java APIs are missing, byte code difference, etc). More details can be found in the following issues: #62, #270, #303.

Bean Validation

  • Easy Random can generate random valid data for all Bean Validation API annotations except for @Digits.
  • Easy Random does not support Meta-annotations. This is due to the fact that it is not possible to "combine" two (or more) randomizers for a single field (in the end, a single randomizer will be selected to randomize each field). Adding support for a meta-annotation can be done manually by registering a custom randomizer using a field definition like: FieldPredicates.ofType(MyFieldType.class).and(FieldPredicates.isAnnotatedWith(MyMetaAnnotation.class))
  • Due to type erasure, Easy Random is not able to introspect and hence generate valid data for annotated parametrized types like Set<@Size(min = 1, max = 10) String> listOfStrings; (see #372). A custom randomizer should be used for such fields.

Inner classes and static nested classes

Easy Random is able to randomize inner classes as well as static nested classes. However, the constructor of an inner class will not be called as Easy Random is not be able to get this constructor through reflection. In this case, it will fallback to using Objenesis (which will instantiate the inner class without calling the constructor). So if you have some initialization code in the constructor of an inner class, do not expect it to be called. Otherwise, you need to make your inner class as a static nested class for this to work. For more details, take a look at issue #268.