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

Stop generating random values #863

Closed praveenfun2 closed 1 month ago

praveenfun2 commented 8 months ago

Describe your question

Is there a way by which we can disable prepopulating the fields with random values.

seongahjo commented 8 months ago

@praveenfun2 Hello, it would be great with a detailed example. Is it for a specific test case or all test cases?

There are several ways to disable pre-population. Here are some common ways you can handle this issue.

  1. set API in ArbitraryBuilder would prevent the generation of a random value. It will work in all test cases if you use this API in register option. If it is too complex to generate, then use set API with Values.just.
  2. You can use pushArbitraryIntrospector option
  3. You can use pushPropertyGenerator option to remove the properties that are not random values. But in this case, you can not use set API.
praveenfun2 commented 8 months ago

So I want to disable this feature altogether, as if it was not even present. I have an existing setup of test cases where we don't populate the values randomly. We populate the values by hardcoding it. So the values which are not harcoded, I want them to be null.

seongahjo commented 8 months ago

@praveenfun2 If so, you can use defaultNullInjectGenerator option.

FixtureMonkey nullGenerateFixtureMonkey = FixtureMonkey.builder()
            .defaultNullInjectGenerator(
                new DefaultNullInjectGenerator(
                    1.0d,
                    false,
                    false,
                    false,
                    Collections.emptySet(),
                    Collections.emptySet()
                )
            )
            .build();

nullGenerateFixtureMonkey will generate all properties as null if no customization APIs are used.


Or You can use your existing test setup just as below.

fixtureMonkey.giveMeBuilder(YourExistingTestObject)
    .sample()

FixtureMonkey instance will generate an object exactly like your existing test object, without populating it with random values. You can also customize it just like any other instance Fixture Monkey generates.

seongahjo commented 1 month ago

Let me know if any more questions!