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

[Kotlin] Primary Constructor에 포함되지 않는 프로퍼티 초기화 하는 방법 #1009

Open
jayyhkwon opened this issue Jul 18, 2024 · 1 comment
Labels
question Further information is requested

Comments

@jayyhkwon
Copy link

jayyhkwon commented Jul 18, 2024

안녕하세요, 우선 좋은 라이브러리 제공해주셔서 감사드린다는 인사 먼저 드립니다 ( __ )
덕분에 테스트 작성 시 생산성이 많이 증가하였습니다!

다름이 아니라. Primary Contsturctor에 포함되지 않는 프로퍼티의 경우 항상 null로 초기화 됩니다.
PrimaryConstructorArbitraryIntrospector에 의해 생성자에 존재하는 필드만 조작해서 그런걸로 추측되는데요.

data class TestObject(
  val a: String?,
  val b: Long?
) {
  var c: String? = null // 항상 null
}

해당 클래스 뿐만 아니라, 글로벌하게 생성자에 포함되지 않는 필드도 초기화 할 수 있는 방법을 가이드 받을 수 있을까요??

@jayyhkwon jayyhkwon added the question Further information is requested label Jul 18, 2024
@jayyhkwon jayyhkwon changed the title [Kotlin] 기본타입 제약 추가하기 [Kotlin] Primary Constructor에 포함되지 않는 프로퍼티 초기화 하는 방법 Jul 18, 2024
@seongahjo
Copy link
Contributor

seongahjo commented Jul 19, 2024

@jayyhkwon
안녕하세요!
아래와 같이 코틀린 기본 생성 방법인 PrimaryConstructorArbitraryIntrospector.INSTANCE KotlinPropertyArbitraryIntrospector.INSTANCE를 섞어서 사용하시면 생성자에 포함하지 않은 필드도 초기화할 수 있습니다.

val fixture = FixtureMonkey.builder()
            .plugin(KotlinPlugin())
            .objectIntrospector(
                CompositeArbitraryIntrospector(
                    listOf(
                        PrimaryConstructorArbitraryIntrospector.INSTANCE,
                        KotlinPropertyArbitraryIntrospector.INSTANCE
                    )
                )
            )
            .build()

혹시 이슈 있으시면 편하게 말씀해주세요.

감사합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants