naver / fixture-monkey

Let Fixture Monkey generate test instances including edge cases automatically
https://naver.github.io/fixture-monkey
Apache License 2.0
560 stars 89 forks source link

FixtureMonkey in Kotlin (sealed class + object) #867

Closed kdohyeon closed 8 months ago

kdohyeon commented 8 months ago

Describe your question

Tell us what kind of problem you are having.

I am having difficulty applying FixtureMonkey with Kotlin's sealed class and object.

Please consider the below code:

sealed class SealedClassA(
  val valueA: String,
  val valueB: String,
) {
  object OBJECTA : SealedClassA(
    valueA = "123",
    valueB = "456"
  )
}
data class DataClassA(
  val sealedClassA: SealedClassA
)
val fixtureMonkey = FixtureMonkey.builder().plugins(KotlinPlugin()).build()

val mockData = fixtureMonkey.giveMeBuilder<DataClassA>().sample()
bound must be positive

Did I miss out some configs? Anybody faced some similuar issue like this?

Any kind of help would be really appreciated.

Thank you!

seongahjo commented 8 months ago

@kdohyeon Hello. Currently, object inheriting a sealed class is not supported, as it is a constant that does not need to populate random properties.

There is an alternative way to generate it.

 FixtureMonkey.builder()
                .plugin(KotlinPlugin())
                .pushAssignableTypeObjectPropertyGenerator<SealedClassA> { context ->
                    InterfaceObjectPropertyGenerator(listOf(SealedClassA.OBJECTA::class.java))
                        .generate(context)
                }
                .pushAssignableTypeArbitraryIntrospector<SealedClassA> {
                    ArbitraryIntrospectorResult(
                        CombinableArbitrary.from(SealedClassA.OBJECTA),
                    )
                }
                .build()

It will be supported in version 1.1.0.

Thank you.

kdohyeon commented 8 months ago

Oh, thank you for the guide code.

It works well. 👍