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

코틀린 primary constructor가 private일 경우 초기화 불가능합니다. #847

Closed jinia91 closed 9 months ago

jinia91 commented 9 months ago

Describe the bug

Describe your issue here.

타겟 모델

class User private constructor(
    val id: Int,
    var name: String,
    var age: Int,
    var email: String,
    var phone: String,
    var address: String,
    var hobby: String,
    var job: String,
    var etc: String,
    orderItems: MutableList<OrderItem>,
)

테스트 코드

@Test
    fun `ddd user - 주문을 취소할 수 있어야 한다 - fixture monkey`() {
        // given
        val mf = FixtureMonkey.builder()
            .plugin(KotlinPlugin())
            .build()

        val orderItem = OrderItem(
            id = 1,
            name = "item1",
            price = 1000,
            quantity = 1,
        )

        val data = mf.giveMeBuilder<User>()
//            .instantiateBy { constructor() }
            .setExp(User::id, 1)
            .setExp(User::name, "jinia")
            .setExp(User::orderItems, listOf(orderItem))
            .sample()

        // when
        data.email = "tobe"
        data.cancelItem(orderItem)

        // then
        assertEquals("tobe", data.email)
        assertEquals(0, data.orderItems.size)
    }

Your environment

plugins {
    id("org.springframework.boot") version "3.2.0"
    id("io.spring.dependency-management") version "1.1.4"
    kotlin("jvm") version "1.9.20"
    kotlin("plugin.spring") version "1.9.20"
}

group = "org.example"
version = "0.0.1-SNAPSHOT"

java {
    sourceCompatibility = JavaVersion.VERSION_17
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("jakarta.validation:jakarta.validation-api:3.0.0")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("com.navercorp.fixturemonkey:fixture-monkey-starter-kotlin:1.0.5")
}

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

tasks.withType<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

image

private constructor 접근 불가

instantiateBy 를 통해 지정시에는 private이여도 생성 가능하나, givenMeBuilder로 기본 접근시엔 불가능합니다.