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

Default final fields are randomized when using Lombok @Builder #364

Closed
NickUK opened this issue Jul 5, 2019 · 1 comment
Closed

Default final fields are randomized when using Lombok @Builder #364

NickUK opened this issue Jul 5, 2019 · 1 comment

Comments

@NickUK
Copy link

NickUK commented Jul 5, 2019

Using easy-random-core:4.0.0

When using lombok @Builder with a class (like the one below), some of the final fields with default values are randomized:

@Builder
@Data
public class BeanWithDataAndBuilder {

    private final Integer defaultInteger = 1;

    private final int defaultInt = 1;

    private final String defaultString = "default";

    private final Boolean defaultBoolean = false;

    private final boolean defaultBool = false;

    private String randomizeMe;

}

I would expect the following code to pass:

        // Given
        EasyRandomParameters parameters = new EasyRandomParameters().overrideDefaultInitialization(false).collectionSizeRange(1, 1);
        EasyRandom easyRandom = new EasyRandom(parameters);

        // When
        BeanWithDataAndBuilder bean = easyRandom.nextObject(BeanWithDataAndBuilder.class);

        // Then
        assertThat(bean.getDefaultInteger()).isEqualTo(1);
        assertThat(bean.getDefaultInt()).isEqualTo(1);
        assertThat(bean.getDefaultString()).isEqualTo("default");
        assertThat(bean.getDefaultBoolean()).isEqualTo(false);
        assertThat(bean.isDefaultBool()).isEqualTo(false);
        assertThat(bean.getRandomizeMe()).isNotNull();

However all the fields get randomized.

I did find a way of creating this class:

        BeanWithDataAndBuilder.BeanWithDataAndBuilderBuilder tmpBean = easyRandom.nextObject(BeanWithDataAndBuilder.BeanWithDataAndBuilderBuilder.class);
        BeanWithDataAndBuilder bean = tmpBean.build();

This does work as expected. Perhaps the builder class could be detected? I think the issue is that the objenesis generated constructor that is being used doesn't pick up any of the defaults.

@fmbenhassine
Copy link
Member

I was able to reproduce the case. Easy Random tries to create an instance of the target bean using the default constructor (A Java Bean should provide a default constructor btw), and falls back to objenesis if no such constructor exists.

However, looks like this is a bug in lombok: combining @Data with @Builder removes the default constructor (See projectlombok/lombok#816).

Trying to add @NoArgsConstructor results in a compilation error (reported in projectlombok/lombok#1389).

So when this combination of lombok annotations is fixed, Easy Random should work as expected.

I'm closing this as duplicate of #228 (See known limitations section).

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

2 participants