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 90 forks source link

Issue Excluding classes from generation #1016

Open lfcazambuja opened 1 month ago

lfcazambuja commented 1 month ago

Hi!

I'm using Fixture Monkey version 1.0.4. I updated it to the latest version, but some of my unit tests started to fail.

Basically I have a class excluded from generation, like the example shown here.

The problem is that I want to exclude the field from generation, but I need to manually set the respective field in one specific situation. And it was working until the version 1.0.9. After this version, the behavior was modified, and it'snot working anymore.

Taking the example of the link:

@Test
void testExcludeClass() {
    FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
        .addExceptGenerateClass(String.class)
        .build();

    String actual = sut.giveMeOne(Product.class)
      .getProductName();

    then(actual).isNull();
}

the test will fail if I change it to

@Test
void testExcludeClass() {
    FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
                .objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE)
                .addExceptGenerateClasses(String.class)
                .defaultNotNull(true)
                .build();

    String product = fixtureMonkey.giveMeBuilder(Product.class)
      .set("productName", "some-name")
      .sample();

    then(product.getProductName()).isEqualTo("some-name");
}

product.getProductName() is null in this scenario.

It's ok for me having the field with null value if I don't explicitly set it. But I want the possibility to set it with a given value, which is not allowed anymore.

Please bear in mind that I've excluded a String field just to follow the given example. In my scenario I have a complex type instead of a String.

My question is: is that a bug or a feature?

Thanks in advance.

seongahjo commented 1 month ago

@lfcazambuja Hello.

My question is: is that a bug or a feature?

It is a feature. You can find out the following PR in here

In your situation, I recommend that you use Just. It forces the value to be set.

@Test
void testExcludeClass() {
    FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
                .objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE)
                .addExceptGenerateClasses(String.class)
                .defaultNotNull(true)
                .build();

    Product product = fixtureMonkey.giveMeBuilder(Product.class)
      .set("productName", Values.just("some-name"))
      .sample();

    then(product.getProductName()).isEqualTo("some-name");
}

Let me know if you have any further problems.

Thank you.