naver / fixture-monkey

Let Fixture Monkey generate test instances including edge cases automatically
https://naver.github.io/fixture-monkey
Apache License 2.0
575 stars 90 forks source link

Fail test depending on the constructor declaration order #920

Closed DuhanMo closed 9 months ago

DuhanMo commented 9 months ago

Describe the bug

I don't want to use @ConstructorProperties. The problem occurs when a default constructor without parameters and a constructor with parameters exist at the same time.

Tests fail depending on the constructor declaration order. (I think there is a problem with retrieving class information through reflection)

Am I using it the wrong way?

Your environment

Steps to reproduce

...
    @Test
    void testFailDependingOnConstructorDeclarationOrder() {
        FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
                .objectIntrospector(ConstructorPropertiesArbitraryIntrospector.INSTANCE)
                .build();

        Sample actual = fixtureMonkey.giveMeBuilder(Sample.class)
                .set("field1", "foo")
                .sample();

        then(actual.getField1()).isEqualTo("foo");
    }

    static class Sample {
        private String field1;
        private String field2;

        public Sample() {
        }

        public Sample(String field1, String field2) {
            this.field1 = field1;
            this.field2 = field2;
        }

        public String getField1() {
            return field1;
        }

        public String getField2() {
            return field2;
        }
    }
...

Expected behaviour

Pass above test

Actual behaviour

Fail, but public Sample(String field1, String field2) .. is at the top, it will passed

DuhanMo commented 9 months ago

I solved this problem. using this 😭

FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
                .objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE)
                .build();

Sorry for the confusion.