diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt index e9b610900..1e8d22c87 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt @@ -32,6 +32,7 @@ import kotlin.reflect.KClass import kotlin.reflect.KFunction import kotlin.reflect.KParameter import kotlin.reflect.full.primaryConstructor +import kotlin.reflect.jvm.isAccessible @API(since = "0.4.0", status = MAINTAINED) class PrimaryConstructorArbitraryIntrospector : ArbitraryIntrospector { @@ -54,6 +55,7 @@ class PrimaryConstructorArbitraryIntrospector : ArbitraryIntrospector { val constructor = CONSTRUCTOR_CACHE.computeIfAbsent(type) { requireNotNull(kotlinClass.primaryConstructor) { "No kotlin primary constructor provided for $kotlinClass" } } + constructor.isAccessible = true val arbitrariesByPropertyName = it.mapKeys { map -> map.key.objectProperty.property.name } diff --git a/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/InstantiatorTest.kt b/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/InstantiatorTest.kt index 8359a2fb8..a095498e9 100644 --- a/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/InstantiatorTest.kt +++ b/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/InstantiatorTest.kt @@ -5,6 +5,7 @@ import com.navercorp.fixturemonkey.api.instantiator.Instantiator import com.navercorp.fixturemonkey.api.instantiator.Instantiator.constructor import com.navercorp.fixturemonkey.kotlin.KotlinPlugin import com.navercorp.fixturemonkey.kotlin.giveMeBuilder +import com.navercorp.fixturemonkey.kotlin.giveMeOne import com.navercorp.fixturemonkey.kotlin.instantiator.instantiateBy import com.navercorp.fixturemonkey.tests.TestEnvironment.TEST_COUNT import org.assertj.core.api.BDDAssertions.then @@ -446,6 +447,16 @@ class InstantiatorTest { then(actual).isNotNull() } + @RepeatedTest(TEST_COUNT) + fun instantiatePrivateConstructorWithoutInstantiate() { + class PrivateConstructorObject private constructor(val value: String) + + val actual = SUT.giveMeOne() + .value + + then(actual).isNotNull() + } + @RepeatedTest(TEST_COUNT) fun instantiateDefaultArgumentConstructor() { class ConstructorObject(val value: String = "default")