kotlinfixture
kotlinfixture copied to clipboard
Override nullability strategy
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
Any update about the issue?
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)
}