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

Can't set a property value which represents an object of a subclass of the property type. #856

Closed praveenfun2 closed 8 months ago

praveenfun2 commented 8 months ago

Describe the bug

Consider the below code.

    // java
    @Data
    public class Class {
        private String pp;
    }

    @Data
    public class SubClass extends Class {
        private String cc;
    }

    @Value
    @Builder
    public class Sample {
        Class abc;
    }
    // java

    // kotlin
    fun createFixtures() {
        var fixtureMonkey: FixtureMonkey = FixtureMonkey.builder()
            .objectIntrospector(FailoverIntrospector(
                    listOf(
                            BuilderArbitraryIntrospector.INSTANCE,
                            ConstructorPropertiesArbitraryIntrospector.INSTANCE,
                            BeanArbitraryIntrospector.INSTANCE
                    )
            ))
            .build()

        val subclass: SubClass = fixtureMonkey.giveMeOne(SubClass::class.java);
        val sample: Sample = fixtureMonkey.giveMeBuilder(Sample::class.java)
                .fixed()
                .set("abc",  subclass)
                .sample()
    }
    // kotlin

Above code throws error "No resolved property is found."

Your environment

Steps to reproduce

Mentioned above

Expected behaviour

Since property "abc" is of type Class, any subclass (SubClass) should be assignable to it.

Actual behaviour

Tell us what happens instead

praveenfun2 commented 8 months ago

Probably the line below needs to be changed.

https://github.com/naver/fixture-monkey/blob/d384a5bdc3ffb7cdb35d0d6388ea76e91e32aac8/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/customizer/NodeSetDecomposedValueManipulator.java#L154

property -> isAssignable(value.getClass(), Types.getActualType(property.getType())) 
seongahjo commented 8 months ago

@praveenfun2 Thank you for reporting an issue! Yes you're right. It is my mistake it should be fixed in this commit If you don't mind, could you make a PR for dealing with this issue?

praveenfun2 commented 8 months ago

Hi seongahjo, it'll be difficult for me to make any changes to this repo as our organization uses gitlab & I can't login to github via my personal account. I have raised this issue via my personal laptop & it doesn't have the developer environment setup.

seongahjo commented 8 months ago

Oh I see, It'll be fixed in 1.0.8. If you are in a hurry, you can do the following. set("abc", Values.just(subclass))

I'll let you know when it's published.

praveenfun2 commented 8 months ago

Would it work for setLazy ? I just tried but it doesn't seem to work.

seongahjo commented 8 months ago

@praveenfun2 In this case, you can use the API like as below.

set(
    YourExpression,
    Values.just(CombinableArbitrary.from(LazyArbitrary.lazy(SUPPLIER)))
)
seongahjo commented 8 months ago

@praveenfun2 1.0.8 is released. Please check it out!

praveenfun2 commented 8 months ago

Seems to be working !! This can be closed now