There are many instances while writing test cases, where we wanted a baked object with some default values, which we then modify partially as per the need for the test.
Random beans exactly achieves this
I have a Pnl object which has PnlKey. For my test, I need test data with different key elements - date, book, account, etc - so that I can check if the aggregation on those levels works perfectly.
So for the test setup, I would want to create a lot of Pnl objects with only certain concerned attributes with values, say,
date - last 5 days
book - from 1 to 10
etc
And how it is accessed/tested here
Random beans exactly achieves this
I have a Pnl object which has PnlKey. For my test, I need test data with different key elements - date, book, account, etc - so that I can check if the aggregation on those levels works perfectly.
So for the test setup, I would want to create a lot of Pnl objects with only certain concerned attributes with values, say,
date - last 5 days
book - from 1 to 10
etc
public class Pnl { private PnlKey pnlKey; }
public class PnlKey { private String custodianAccount; private Integer bookId; private Integer pnlSpn; private Date date; private Integer bundleId; }
@Bean public EnhancedRandom getEnhancedRandomForPnl() { Randomizer<Date> dateRandomizer = () -> { int max = dates.length; Random random = new Random(); return dates[random.nextInt(max)]; }; Randomizer<Integer> bookRandomizer = () -> { int min = 1; int max = 10; Random random = new Random(); return min + random.nextInt((max - min) + 1); }; Randomizer<Integer> bundleRandomizer = () -> { int min = 11; int max = 20; Random random = new Random(); return min + random.nextInt((max - min) + 1); }; Randomizer<String> custodianAccountRandomizer = () -> { Random random = new Random(); return custodianAccounts[random.nextInt(custodianAccounts.length)]; }; EnhancedRandom enhancedRandom = EnhancedRandomBuilder.aNewEnhancedRandomBuilder() .randomize(Date.class, dateRandomizer) .randomize(new FieldDefinition<>("bookId", Integer.class, PnlKey.class), bookRandomizer) .randomize(new FieldDefinition<>("bundleId", Integer.class, PnlKey.class), bundleRandomizer) .randomize(new FieldDefinition<>("custodianAccount", String.class, PnlKey.class), custodianAccountRandomizer) .build(); return enhancedRandom; }You can check how this is configured here
And how it is accessed/tested here
No comments:
Post a Comment