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

Incorrect behaviour of overrideDefaultInitialization when there is no default constructor #228

Closed
fmbenhassine opened this issue Jan 23, 2017 · 4 comments
Labels
Milestone

Comments

@fmbenhassine
Copy link
Member

When there is no default constructor in the randomized type, default initialization in the constructor is overridden (ie fields are randomizerd) even when the overrideDefaultInitialization is not set.

Here is a failing test with v3.4.0:

@Data
public static class BeanWithDefaultFieldValuesAndNoDefaultConstructor {
    private Long id;
    private String defaultNonNullValue = "default";
    private String defaultNonNullValueSetByConstructor;

    public BeanWithDefaultFieldValuesAndNoDefaultConstructor(Long id) {
        this.id = id;
        defaultNonNullValueSetByConstructor = "defaultSetByConstructor";
    }
}

@Test
public void whenOverrideDefaultInitializationIsFalseAndThereIsNoDefaultConstructor_thenShouldKeepDefaultFieldValues() {
    // Given
    enhancedRandom = aNewEnhancedRandomBuilder().overrideDefaultInitialization(false).build();

    // When
    BeanWithDefaultFieldValuesAndNoDefaultConstructor bean = enhancedRandom.nextObject(BeanWithDefaultFieldValuesAndNoDefaultConstructor.class);

    // Then
    assertThat(bean.getId()).isNotNull();
    assertThat(bean.getDefaultNonNullValue()).isEqualTo("default");
    assertThat(bean.getDefaultNonNullValueSetByConstructor()).isEqualTo("defaultSetByConstructor");
}

What happens behind the scene is that:

  • random beans does not find a default constructor, it delegates object creation to objenesis
  • objenesis by-passes the constructor and creates a new instance of the bean (here, all fields are null)
  • random beans finds that all fields are null, hence it randomizes fields (which is not what we want to do, since we didn't set the overrideDefaultInitialization parameter)
@fmbenhassine
Copy link
Member Author

fmbenhassine commented Jan 26, 2017

Not sure if this issue is valid. Non default constructor will never be called (by objenesis).

The only trick I see, when there is no default constructor, is to try to call (one of) the existing constructor(s) with random parameters.. But this is too much work IMO and not worth it. Random Beans was designed to randomize Java beans, which by definition, provide a default constructor.

@fmbenhassine
Copy link
Member Author

@PascalSchumacher What do you think?

Should we keep the behaviour of overrideDefaultInitialization as is and update documentation to add a note on default constructor?

@PascalSchumacher
Copy link
Collaborator

I agree that adding support for calling non-default constructors would require serious work and I'm not sure how useful it is.

Let's keep and document the current behavior for now.

@fmbenhassine fmbenhassine added invalid and removed bug labels Jan 27, 2017
@fmbenhassine
Copy link
Member Author

Great, I will update the documentation about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants