I was looking for some utility class/code that would take a java bean and initialize all its values to random values. It could be done via reflection as some libraries already create the toString() or equals() methods. Is is useful while developing the UI to have some data for example.
Other possible nice to haves:
somebody knows something like this? thanks
EDIT: resolving...Got Apocalisp's sample working and definitively is what I was looking for. It has some drawbacks IMHO:
thanks!
Take a look at the Gen class from the Reductio library. This is part of a highly configurable framework for generating arbitrary values of more or less any type. Generators for primitive types and most of the Java collections classes are provided. You should be able to create Arbitrary
instances for your classes fairly easily.
EDIT Here's the example, corrected:
import static fj.test.Arbitrary.*;
import static fj.Function.*;
static final Arbitrary<Person> personArbitrary =
arbitrary(arbInteger.gen.bind(arbString.gen, arbBoolean.gen,
curry(new F3<Integer, String, Boolean, Person>() {
public Person f(final Integer age, final String name, final Boolean male)
{return new Person(age, name, male);}})));
Then generate an arbitrary Person of "size 100" like so. I.e. it will have a name of 100 characters at most.
Person p = personArbitrary.gen.gen(100, Rand.standard);