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

Failed to generate fixtures for Kotlin sealed class containing object declaration due to missing primary constructor #1132

Open
forgreen0624 opened this issue Dec 23, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@forgreen0624
Copy link

Describe the bug

When using the FixtureMonkey library to generate fixtures for a Kotlin sealed class, the process fails if the sealed class contains an object declaration. The error message indicates that a primary constructor is not provided for the object class, which leads to fixture generation failure.

Your environment

  • version of Fixture Monkey: 1.1.6
  • version of Java/Kotlin : kotlin, 2.0.20

Steps to reproduce

class FixtureMonkeyTest {

    @Test
    fun `test fixtureMonkey`() {
        val sampled = FixtureMonkeyBuilder()
            .plugin(KotlinPlugin())
            .build()
            .giveMeKotlinBuilder<MyData>()
            .set(MyData::name, "my_name")
            .sample()

        println("sampled: $sampled")

    }

    data class MyData(
        val id: Int,
        val name: String,
        val scheme: Scheme,
    )

    sealed class Scheme {
        data class Url(val scheme: String) : Scheme()
        data object None : Scheme()
    }
}

Expected behaviour

scheme property in MyData class could be initialized with arbitrarily one of Scheme.Url(String). Scheme.None

Actual behaviour

No kotlin primary constructor provided for class com.fixturetest.FixtureMonkeyTest$Scheme$None
java.lang.IllegalArgumentException: No kotlin primary constructor provided for class com.fixturetest.FixtureMonkeyTest$Scheme$None
	at com.navercorp.fixturemonkey.kotlin.type.KotlinTypeCacheKt$kotlinPrimaryConstructor$1.invoke(KotlinTypeCache.kt:51)
	at com.navercorp.fixturemonkey.kotlin.type.KotlinTypeCacheKt$kotlinPrimaryConstructor$1.invoke(KotlinTypeCache.kt:49)
	at com.navercorp.fixturemonkey.kotlin.type.KotlinTypeCacheKt.kotlinPrimaryConstructor$lambda$4(KotlinTypeCache.kt:49)
	at java.base/java.util.Map.computeIfAbsent(Map.java:1054)
	at com.navercorp.fixturemonkey.kotlin.type.KotlinTypeCacheKt.kotlinPrimaryConstructor(KotlinTypeCache.kt:49)
	at com.navercorp.fixturemonkey.kotlin.introspector.PrimaryConstructorArbitraryIntrospector$Companion$PROPERTY_GENERATOR$1.invoke(PrimaryConstructorArbitraryIntrospector.kt:87)
	at com.navercorp.fixturemonkey.kotlin.introspector.PrimaryConstructorArbitraryIntrospector$Companion$PROPERTY_GENERATOR$1.invoke(PrimaryConstructorArbitraryIntrospector.kt:86)
	at com.navercorp.fixturemonkey.kotlin.property.KotlinConstructorParameterPropertyGenerator$generateChildProperties$1.invoke(KotlinConstructorParameterPropertyGenerator.kt:45)
	at com.navercorp.fixturemonkey.kotlin.property.KotlinConstructorParameterPropertyGenerator$generateChildProperties$1.invoke(KotlinConstructorParameterPropertyGenerator.kt:44)
	at com.navercorp.fixturemonkey.kotlin.property.KotlinConstructorParameterPropertyGenerator.generateChildProperties$lambda$0(KotlinConstructorParameterPropertyGenerator.kt:44)
	at java.base/java.util.Map.computeIfAbsent(Map.java:1054)
	at com.navercorp.fixturemonkey.kotlin.property.KotlinConstructorParameterPropertyGenerator.generateChildProperties(KotlinConstructorParameterPropertyGenerator.kt:44)
	at com.navercorp.fixturemonkey.api.property.CompositePropertyGenerator.generateChildProperties(CompositePropertyGenerator.java:57)
	at com.navercorp.fixturemonkey.builder.ArbitraryBuilderContext.lambda$initializeResolvedPropertyGenerator$2(ArbitraryBuilderContext.java:310)
	at com.navercorp.fixturemonkey.api.property.LazyPropertyGenerator.lambda$null$0(LazyPropertyGenerator.java:50)
	at com.navercorp.fixturemonkey.api.lazy.UnSafeLazyArbitraryImpl.getValue(UnSafeLazyArbitraryImpl.java:53)
	at com.navercorp.fixturemonkey.api.property.LazyPropertyGenerator.generateChildProperties(LazyPropertyGenerator.java:51)
	at com.navercorp.fixturemonkey.api.tree.TreeProperty.lambda$toArbitraryProperty$0(TreeProperty.java:85)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
	at com.navercorp.fixturemonkey.api.tree.TreeProperty.toArbitraryProperty(TreeProperty.java:87)
	at com.navercorp.fixturemonkey.api.tree.DefaultTraverseNode.generateObjectNode(DefaultTraverseNode.java:335)
	at com.navercorp.fixturemonkey.api.tree.DefaultTraverseNode.generateChildrenNodes(DefaultTraverseNode.java:464)
	at com.navercorp.fixturemonkey.api.tree.DefaultTraverseNode.lambda$expand$1(DefaultTraverseNode.java:230)
	at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:273)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
	at com.navercorp.fixturemonkey.api.tree.DefaultTraverseNode.expand(DefaultTraverseNode.java:239)
	at com.navercorp.fixturemonkey.tree.ObjectNode.expand(ObjectNode.java:70)
	at com.navercorp.fixturemonkey.tree.MetadataCollector.collect(MetadataCollector.java:66)
	at com.navercorp.fixturemonkey.tree.MetadataCollector.collect(MetadataCollector.java:54)
	at com.navercorp.fixturemonkey.tree.ObjectTree.<init>(ObjectTree.java:48)
	at com.navercorp.fixturemonkey.resolver.ArbitraryResolver.lambda$resolve$2(ArbitraryResolver.java:69)
	at com.navercorp.fixturemonkey.api.lazy.UnSafeLazyArbitraryImpl.getValue(UnSafeLazyArbitraryImpl.java:53)
	at com.navercorp.fixturemonkey.resolver.ResolvedCombinableArbitrary.lambda$new$0(ResolvedCombinableArbitrary.java:63)
	at com.navercorp.fixturemonkey.api.lazy.UnSafeLazyArbitraryImpl.getValue(UnSafeLazyArbitraryImpl.java:53)
	at com.navercorp.fixturemonkey.resolver.ResolvedCombinableArbitrary.combined(ResolvedCombinableArbitrary.java:75)
	at com.navercorp.fixturemonkey.builder.DefaultArbitraryBuilder.sample(DefaultArbitraryBuilder.java:483)
	at com.navercorp.fixturemonkey.kotlin.KotlinTypeDefaultArbitraryBuilder.sample(FixtureMonkeyExtensions.kt:245)
	at com.fixturetest.FixtureMonkeyTest.test fixtureMonkey(FixtureMonkeyTest.kt:17)
	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:569)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:112)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:40)
	at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:54)
	at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:53)
	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:569)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
	at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:92)
	at jdk.proxy1/jdk.proxy1.$Proxy4.processTestClass(Unknown Source)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker$2.run(TestWorker.java:181)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:130)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:101)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:61)
	at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:122)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:69)
	at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
	at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)
@forgreen0624 forgreen0624 added the bug Something isn't working label Dec 23, 2024
@seongahjo
Copy link
Contributor

@forgreen0624
Hi.

I cannot reproduce the case. Can you check it if it is the right case?

image

Thank you.

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