easy-random icon indicating copy to clipboard operation
easy-random copied to clipboard

Custom field Randomizer is not picked up when using randomListOf

Open raspacorp opened this issue 9 years ago • 2 comments

I am trying to use a custom Randomizer when generating a List of objects, the randomizer will generate consecutive integer values inside the campaignCode field, it works well if I create the objects individually using nextObject method but not when using the bulk generation with randomListOf or randomCollectionOf.

I am using random-beans version: 3.5.0

This is the test:

User.java

public class User {
    private Long id;
    private String name;
    private String campaignCode;

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getCampaignCode() { return campaignCode; }
    public void setCampaignCode(String campaignCode) { this.campaignCode = campaignCode; }
}

RandomBeansTest.java

public class RandomBeansTest {
    Integer numberToGenerate = 100;
    EnhancedRandom enhancedRandom;

    @Before
    public void setUp() throws Exception {
        enhancedRandom = EnhancedRandomBuilder.aNewEnhancedRandomBuilder().randomize(FieldDefinitionBuilder.field()
                        .named("campaignCode").ofType(String.class).inClass(User.class).get(),
                new Randomizer<String>() {
                    int current = 0;

                    @Override
                    public String getRandomValue() {
                        return String.valueOf(current++);
                    }
                }).build();
    }

    @Test
    public void testNextObjectWithRandomizer() {
        User user = enhancedRandom.nextObject(User.class, "id");
        Integer campaignCode = null;

        try {
            campaignCode = Integer.parseInt(user.getCampaignCode());
            Assert.assertNull(user.getId());
            Assert.assertTrue(campaignCode == 0);
            campaignCode = Integer.parseInt(user1.getCampaignCode());
            Assert.assertNull(user1.getId());
            Assert.assertTrue(campaignCode == 1);
        } catch(NumberFormatException e) {
            Assert.fail("Generated campaignCode doesn't represent an integer value: "+user.getCampaignCode());
        }
    }

    @Test
    public void testRandomListWithRandomizer() {
        List<User> users = enhancedRandom.randomListOf(numberToGenerate, User.class, "id");
        Integer campaignCode = null;

        for(User generatedUser : users) {
            Assert.assertNull(generatedUser.getId());

            try {
                campaignCode = Integer.parseInt(generatedUser.getCampaignCode());
            } catch(NumberFormatException e) {
                Assert.fail("Generated campaignCode in List doesn't represent an integer value: "+generatedUser.getCampaignCode());
            }

            Assert.assertTrue(campaignCode < numberToGenerate && campaignCode >= 0);
        }
    }
}

When running the tests the one that uses nextObject passes but not the one using the randomListOf, the assertion fails as the generator is not creating a valid integer value.

...
java.lang.AssertionError: Generated campaignCode in List doesn't represent an integer value: OXsuTg
...

raspacorp avatar May 09 '17 04:05 raspacorp

Hi @raspacorp,

randomListOf and randomCollectionOf are static methods, so they do not use the configuration created by EnhancedRandomBuilder.

You can use objects to generate a Stream and convert it into the required collection type, e.g. enhancedRandom.objects(numberToGenerate, User.class, "id").collect(Collectors.toList())

I hope this helps.

Cheers, Pascal

Edit: By the way you should update to random-beans 3.6.0 :wink:

PascalSchumacher avatar May 09 '17 06:05 PascalSchumacher

My bad, I overlooked the static nature of those methods, thanks for the input. Will update to 3.6.0 as well, thanks.

raspacorp avatar May 09 '17 15:05 raspacorp