naver / fixture-monkey

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

Random object generation with ArbitraryBuilder::sampleList #778

Closed lfcazambuja closed 11 months ago

lfcazambuja commented 11 months ago

Hi, everyone!

I have the given interface:

package test;

public interface ExampleDto {

    Long getId();

}

and the given unit test, using JUnit 5:

    @Test
    public void testRandomIdGeneration() {
        //Given
        int collectionSize = new Random().nextInt(10) + 1;
        FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
                .objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE)
                .defaultNotNull(true)
                .build();
        ArbitraryBuilder<ExampleDto> builder = fixtureMonkey.giveMeBuilder(ExampleDto.class);
        List<ExampleDto> exampleDtos = builder.sampleList(collectionSize);

        System.out.println(exampleDtos.stream()
                .map(ExampleDto::getId)
                .collect(Collectors.toList()));

        //when
        Set<Long> longs = exampleDtos.stream()
                .map(ExampleDto::getId)
                .collect(Collectors.toSet());

        //then
        Assertions.assertEquals(collectionSize, longs.size());
    }

I was using the FixtureMonkey 0.5.9. With this version - and previous ones - , the test runs without errors. In other words, the ArbitraryBuilder::sampleList method gives me different instances of ExampleDto, each one with a different ID (as you can see in the System.out.println line if you execute the code.

When I updated the FixtureMonkey version to 0.6.0 (or newer) the test started to fail. The instances of ExampleDto, all of them, are being created with the same ID, thus failing the test.

I want to know if this is an issue or if I should do something different in my code from version 0.6.0 onwards.

Thanks in advance.

seongahjo commented 11 months ago

@lfcazambuja Hello, thank you for reporting an issue. It is a bug in the creation of an interface object. It'll be fixed in 0.6.9. When it is published, I will let you know.

Thank you.

seongahjo commented 11 months ago

@lfcazambuja Hello, 0.6.9 is released. Please check it out. Thank you.

lfcazambuja commented 11 months ago

The problem was solved. Thank you!