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

Override nullability strategy #89

Open
mahdibohloul opened this issue Dec 18, 2021 · 2 comments
Open

Override nullability strategy #89

mahdibohloul opened this issue Dec 18, 2021 · 2 comments

Comments

@mahdibohloul
Copy link

Hi there,

According to your document about overriding nullability strategy, I have an issue with it in nullable properties.

By default, when the library comes across a nullable type, such as String? it will randomly return a value or null. This can be overridden by setting a nullability strategy.

I have a data class like:

data class Order(val id: String? = null, /*with some other fields*/ )

And I'm using KotlinFixture to write tests like that:

val order = kotlinFixture {
      nullabilityStrategy(NeverNullStrategy)
      //Other configuration
    } <Order>()

I expected the id attribute to always have a value. But this is not the case and sometimes it is null and the test is not deterministic and sometimes it is passed and sometimes it is not.

My fixture version: 1.2.0

@mahdibohloul
Copy link
Author

Any update about the issue?

@mattmook
Copy link
Member

mattmook commented Mar 5, 2022

Hi sorry @mahdibohloul I missed seeing your issue previously.

The issue comes as you have both null types and default values. So while the null strategy will ensure it won't generate a null at random, the optional strategy will randomly choose between generating a value or using the default value.

What this means is for this scenario you also need to override the optionalStrategy to be NeverOptionalStrategy:

private val fixture = kotlinFixture {
    nullabilityStrategy(NeverNullStrategy)
    optionalStrategy(NeverOptionalStrategy)
}

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