What library / method can I use for deep generation of random objects for a given class in Java / Scala?
By deep generation I mean recursively filling the entire object graph with random values, for both primitive and complex types.
The goal is to use randomly generated mock objects for tests.
To make the tests consistent and reproducible, the set of generated objects should be identical on every run.
As far as I know there are there are two powerful methods to randomly generate very large, arbitrary types of object. The first is called the recursive method (introduced by Wilf and Nijenhuis, formalized by Flajolet, van Cutsem and Zimmerman), the second is called Boltzmann sampling by Duchon, Flajolet, Louchard and Schaeffer.
In both methods, you give a structure definition (a “grammar”) and you are returned with a large random object which is in accordance with the grammar.
Both have been used very successfully in wide scale testing of programs (similar to Haskell’s QuickCheck). Unfortunately, while there are some various implementations of the methods, I don’t think there is a ready to use library for Java. For example of what can be done, you might want to check Yann Ponty’s page with Java implementations of these methods for bioinformatics. If you were using OCaml, then Alexis Darrasse and Benjamin Canou have implemented a useful subset of Boltzmann sampling as a completely autonomous library for algebraic types (sumtypes).