Skip to content

Instantly share code, notes, and snippets.

@eliasnogueira
Last active November 15, 2020 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliasnogueira/24eabcddd29d95641cf7181d1a3a227d to your computer and use it in GitHub Desktop.
Save eliasnogueira/24eabcddd29d95641cf7181d1a3a227d to your computer and use it in GitHub Desktop.
Example of a Data Factory class with JavaFaker
public class LoanDataFactory {
private static Faker faker;
private static final int MIN_VALID_INSTALLMENTS = 2;
private static final int MAX_VALID_INSTALLMENTS = 18;
private static final int MAX_NUMBER_OF_DECIMALS = 2;
private static final Long MIN_VALID_AMOUNT = Long.valueOf("1000");
private static final Long MAX_VALID_AMOUNT = Long.valueOf("40000");
private LoanDataFactory() {
faker = new Faker();
}
public static Loan createLoan() {
return new LoanBuilder().
name(faker.name().firstName()).
email(faker.internet().emailAddress())
.amount(BigDecimal.valueOf(
faker.number().randomDouble(
MAX_NUMBER_OF_DECIMALS, MIN_VALID_AMOUNT, MAX_VALID_AMOUNT)))
.installments(faker.number().numberBetween(MIN_VALID_INSTALLMENTS, MAX_VALID_INSTALLMENTS)).build();
}
// other methods ignored
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment