Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please have a facility to create random objects that follow the entire object tree. #111

Open
thehybridform opened this issue Jan 12, 2018 · 0 comments

Comments

@thehybridform
Copy link

thehybridform commented Jan 12, 2018

I have this to create random data but it only does simple fields. Not any complex child objects.

public final class RandomObjectGenerator {
    private RandomObjectGenerator() {
    }

    public static <T> T random(Class<T> clazz) {
        PojoClass pojoClass = PojoClassFactory.getPojoClass(clazz);
        T instance = RandomFactory.getRandomValue(clazz);
        pojoClass.getPojoFields().stream().filter(RandomObjectGenerator::canSet).forEach((pojoField) -> {
            pojoField.set(instance, RandomFactory.getRandomValue(pojoField));
        });
        return instance;
    }

    private static boolean canSet(PojoField pojoField) {
        return !pojoField.isFinal() && !pojoField.isStatic() && !pojoField.isSynthetic();
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant