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

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

Closed
msugo1 opened this issue Jan 31, 2024 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@msugo1
Copy link
Contributor

msugo1 commented Jan 31, 2024

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

  • kotlin 1.9.20
  • java 17
  • fixture-monkey 1.0.13

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)
@msugo1 msugo1 added the bug Something isn't working label Jan 31, 2024
@seongahjo seongahjo added this to the 1.0.14 milestone Feb 2, 2024
@seongahjo
Copy link
Contributor

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

@seongahjo
Copy link
Contributor

It is closed because it is fixed in 1.0.14.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants