naver / fixture-monkey

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

내부 value class 프로퍼티의 생성자가 private 일 경우 테스트 객체 생성에 실패합니다. #910

Closed msugo1 closed 8 months ago

msugo1 commented 9 months ago

Describe the bug

대상 모델

data class Product(
    val interests: ProductInterests
)

data class ProductInterests(
    val minRate: ProductInterestRate,
    val maxRate: ProductInterestRate
)

@JvmInline
value class ProductInterestRate private constructor(
    val value: BigDecimal
) {
    companion object {
        fun from(value: BigDecimal): ProductInterestRate = ProductInterestRate(value.setScale(2,RoundingMode.HALF_EVEN))
    }
}

테스트코드

class FixtureMonkeyTest {

    private val fixtureMonkey = FixtureMonkey.builder()
        .plugin(KotlinPlugin())
        .build()

    @Test
    fun `create fixture`() {
        // given
        val builder = fixtureMonkey.giveMeBuilder(Product::class.java)

        // when
        val obj = builder.sample()

        // then
        obj.shouldNotBeNull()
    }
}

위와 같이 value class 가 private constructor를 가질 경우 테스트 객체 생성에 실패합니다.

Your environment

build.gradle.kts


java {
    sourceCompatibility = JavaVersion.VERSION_17
}

dependencies {
    ...
    // fixture
    testImplementation("com.navercorp.fixturemonkey:fixture-monkey-starter-kotlin:1.0.13")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs += "-Xjsr305=strict"
        jvmTarget = "17"
    }
}

tasks.test {
    useJUnitPlatform()
}

Steps to reproduce

Tell us how to reproduce this issue. Or provide us an example repository to check the bug

Expected behaviour

테스트 객체 생성 성공

Actual behaviour

java.lang.IllegalArgumentException: object is not an instance of declaring class

    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at kotlin.reflect.jvm.internal.calls.ValueClassAwareCaller.call(ValueClassAwareCaller.kt:179)
    at kotlin.reflect.jvm.internal.KCallableImpl.callDefaultMethod$kotlin_reflection(KCallableImpl.kt:200)
    at kotlin.reflect.jvm.internal.KCallableImpl.callBy(KCallableImpl.kt:112)
    at com.navercorp.fixturemonkey.kotlin.introspector.PrimaryConstructorArbitraryIntrospector.introspect$lambda$3(PrimaryConstructorArbitraryIntrospector.kt:72)
    at com.navercorp.fixturemonkey.api.arbitrary.ObjectCombinableArbitrary.combined(ObjectCombinableArbitrary.java:54)
    at com.navercorp.fixturemonkey.api.arbitrary.NullInjectCombinableArbitrary.combined(NullInjectCombinableArbitrary.java:46)
    at com.navercorp.fixturemonkey.api.arbitrary.TraceableCombinableArbitrary.combined(TraceableCombinableArbitrary.java:42)
    at com.navercorp.fixturemonkey.api.arbitrary.ObjectCombinableArbitrary.lambda$combined$0(ObjectCombinableArbitrary.java:51)
    at java.base/java.util.HashMap.forEach(HashMap.java:1421)
    at com.navercorp.fixturemonkey.api.arbitrary.ObjectCombinableArbitrary.combined(ObjectCombinableArbitrary.java:50)
    at com.navercorp.fixturemonkey.api.arbitrary.NullInjectCombinableArbitrary.combined(NullInjectCombinableArbitrary.java:46)
    at com.navercorp.fixturemonkey.api.arbitrary.TraceableCombinableArbitrary.combined(TraceableCombinableArbitrary.java:42)
    at com.navercorp.fixturemonkey.api.arbitrary.FilteredCombinableArbitrary.combined(FilteredCombinableArbitrary.java:73)
    at com.navercorp.fixturemonkey.resolver.ResolvedCombinableArbitrary.combined(ResolvedCombinableArbitrary.java:77)
    at com.navercorp.fixturemonkey.resolver.DefaultArbitraryBuilder.sample(DefaultArbitraryBuilder.java:489)
seongahjo commented 9 months ago

@msugo1 Thanks for your PR. It will be included in 1.0.14.

seongahjo commented 8 months ago

It is closed because it is fixed in 1.0.14.