-
Notifications
You must be signed in to change notification settings - Fork 234
known limitations
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)
Easy Random uses the Java Reflection API to introspect the bean type at runtime and generate random data accordingly. There are some cases where Easy Random is not able to get the information required about a class or a field type at runtime. The most obvious use case is generic types (and custom types that extend them). Due to type erasure, generic types are erased at runtime and Easy Random is just unable to get this information. Some related issues to this use case: #19, #242, #311, #375, #380. The suggested way to work around this limitation is to register a custom randomizer for the specific type.
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.
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.
- 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.
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.
Easy Random is created by Mahmoud Ben Hassine with the help of some awesome contributors!