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

Call multiple arbitary introspectors if required #825

Closed esfomeado closed 1 year ago

esfomeado commented 1 year ago

Describe the feature you request

As of now it's possible to call multiple Arbitary Introspectors by using the CompositiveArbitaryIntrospector. This will call all introspectors but for my use case I would like to define multiple introspectors similar to the CompositiveArbitaryIntrospector but only call the introspectors until the object is created and then stop.

Example: CompositiveArbitaryIntrospector( List.of( ConstructorPropertiesArbitaryResolver.INSTANCE, FieldReflectionArbitaryIntrospector.INSTANCE ) ).

Class A - Can only be generated via constructor. So it uses the ConstructorPropertiesArbitaryResolver and it stops. Class B - Can only be generated via reflection. Tries to generate via the ConstructorPropertiesArbitaryResolver fails and goes to the next one FieldReflectionArbitaryIntrospector. The class is generated and then stops

As workaround I have to defined an objectIntrospector and then define per class another one in case it´s different from the default one. Example: objectIntrospector(ConstructorPropertiesArbitaryResolver.INSTANCE) .pushExactTypeArbitaryIntrospector(ClassB.class, FieldReflectionArbitaryIntrospector.INSTANCE

seongahjo commented 1 year ago

@esfomeado Hi, you can use FailoverArbitraryIntrospector. It is the one exactly what you want.

Details here

esfomeado commented 1 year ago

Not sure how I missed that one. That's it thanks.

esfomeado commented 11 months ago

@seongahjo After looking at the FailoverArbitraryIntrospector it doesn't work as expected.

If the class can be generated with both Introspectors it will combine the result which isn´t want I want.

I was expecting the following:

If the class was generated via one introspector return right away otherwise keep trying until it generates.

I will implement this Introspector on my project but I think its's useful to have it by default as well on the lib.

seongahjo commented 11 months ago

@esfomeado

If the class can be generated with both Introspectors it will combine the result which isn´t want I want.

Is there a test case that can be reproduced?

FailoverArbitraryIntrospector should be generate by only one ArbitraryIntrospector.