Skip to content

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

@jinia91

Description

@jinia91

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로 기본 접근시엔 불가능합니다.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions