Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom field Randomizer is not picked up when using randomListOf #254

Closed
raspacorp opened this issue May 9, 2017 · 2 comments
Closed

Custom field Randomizer is not picked up when using randomListOf #254

raspacorp opened this issue May 9, 2017 · 2 comments

Comments

@raspacorp
Copy link

raspacorp commented May 9, 2017

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
...
@PascalSchumacher
Copy link
Collaborator

PascalSchumacher commented May 9, 2017

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 😉

@raspacorp
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants