jqwik icon indicating copy to clipboard operation
jqwik copied to clipboard

Simple Generation of Java Beans

Open jlink opened this issue 5 years ago • 2 comments

Testing Problem

Generating POJOs from their constructor or static factory method in jqwik is rather straightforward through Arbitraries.forType(..). Generating Java beans through property setters requires more effort, e.g. through combinators:

Combinators.withBuilder(MyJavaBean::new)
                .use(anyName()).inSetter(MyJavaBean::setFirstName)
                .use(anyName()).inSetter(MyJavaBean::setLastName)
                .use(anyInitial().injectNull(0.5)).inSetter(MyJavaBean::setInitial)
                ...
                .build();

This requires quite some boilerplate for each property.

Suggested Solution

Provide type based generation of java beans, e.g.

Combinators.forBean(MyJavaBean.class) or Arbitraries.forJavaBeanType(..).

which will try to set all properties using their type as default.

Discussion

Differentiating required/optional bean properties and (partially) specifying dedicated generators for some properties may add quite some complexity to the API.

jlink avatar Sep 21 '20 09:09 jlink

This seems to be on the backburner because of perceived complexity. Would the JavaBeans spec provide a convenient way to tackle this? Maybe something along the lines of iterating through the class's BeanInfo and working with properties that have a write method? Custom PropertyDescriptor implementations could be implemented to hold the generator metadata.

gdupontf avatar Mar 10 '22 16:03 gdupontf

Actually, I don’t think the implementation would be too complicated. It’s just that I haven’t seen demand for it and I haven’t seen a good DSL/API suggestion for it, yet. Do you have a concrete use case? Do you have an API in mind?

jlink avatar Mar 10 '22 18:03 jlink