Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DataFaker - the most powerful fake data generation library

Elias Nogueira
May 05, 2024
92

DataFaker - the most powerful fake data generation library

A hands-on presentation about DataFaker a library to generate fake data for the JVM (Java, Kotlin, Groovy).

Elias Nogueira

May 05, 2024
Tweet

Transcript

  1. ❌ address=XmcWoqivEzSoUwEksGNjlzrjUmWKKCIDOTsDnPAlHPQMJaWYzt ❌ age=0 Meaningful data Generate random fixed data

    ❌ name=John Doe ❌ [email protected] No data requirement focus https://speakerdeck.com/eliasnogueira/managing-test-data
  2. Address (any pieces), latitude, longitude IBAN, BIC, credit card, stock,

    routing number DataFaker Any composition, suffix, prefix Credit card, security code, card type Domain, e-mail, image, IP address, password Different providers to help you! Names Address Finance Business Internet +200 Generating fake data for the JVM (Java, Kotlin, Groovy) has never been easier!
  3. Generation in a nutshell The connections between the YML file

    and the code. Sometimes it has some logic. This file contains the data generated by DataFaker. It supports different locales. YML file Provider class
  4. (My) History DataFaker is a fork of JavaFaker, but with

    faster general and security fixes, open to (serious) contribution, and up-to-date.
  5. Base Providers Generator Code Output Names faker.name().fullName() Clemente Lemke Address

    faker.address().fullAddress() Apt. 984 62551 Brooke Expressway, Chinmouth, WA 55746 Finance faker.finance().iban() NL31RJCK5939950627 Business faker.business().creditCardNumber() 5202-4875-5936-5334 Internet faker.internet().image() https://picsum.photos/640/480 More than 200 providers available
  6. Transformation Schemas CSV JSON SQL YAML XML Java Schema is

    a set of rules describing what should be done to transform data from Datafaker representation to one of the supported formats
  7. Transformation Schemas Faker faker = new Faker(); Schema<String, String> personSchema

    = Schema.of( field("name", () -> faker.name().nameWithMiddle()), field("username", () -> faker.internet().username()), field("email", () -> faker.internet().emailAddress()), field("password", () -> faker.internet().password(8, 12)) );
  8. Transformation Schemas /* * SQL Data Transformation */ SqlTransformer<String> sqlTransformer

    = new SqlTransformer.SqlTransformerBuilder<String>() .batch(5) .tableName("MY_TABLE") .dialect(SqlDialect.POSTGRES) .build(); String output = sqlTransformer.generate(personSchema, 10); /* * CSV Data Transformation */ CsvTransformer<String> csvTransformer = CsvTransformer.<String>builder().header(true).separator(COMMA).build(); String csvOutput = csvTransformer.generate(retrieveSchema(), 5);
  9. Transformation Schemas CSV Output "name","username","email","password" "Myra Lemke Zulauf","giovanni.hartmann","[email protected]","49uv48051" "Elijah Kautzer

    Hudson","carlota.haag","[email protected]","292ka97i5" "Loraine Simonis Dach","renee.ondricka","[email protected]","9vt5tr7i0" "George Stiedemann Roob","idella.crist","[email protected]","4134aqt8q" "Derek Runolfsdottir Spinka","marcel.simonis","[email protected]","r3073b71j7" SQL Output INSERT INTO MY_TABLE ("name", "username", "email", "password") VALUES ('Yuki Osinski Hagenes', 'billie.collins', '[email protected]', 'def314ac7w9'), ('Kenny Kessler Rice V', 'minh.schneider', '[email protected]', '1ij8xjive6'), ('Cory Tromp Konopelski', 'yuko.russel', '[email protected]', 'h94nbfv99'), ('Lonna Borer Casper', 'joyce.mclaughlin', '[email protected]', '33sopr94vg'), ('Mr. Sammy Braun Langworth', 'darren.osinski', '[email protected]', '39j16kik8');
  10. Real Life Usage Tool To randomize meaningful data in the

    Factory Test Data Factory Factory Factory Pattern to generate different data scenarios Model Model object using the Build pattern to hold data
  11. Real Life Usage public final class UserDataFactory { private static

    final Faker FAKER = new Faker(); public static User validUser() { return User.builder() .name(FAKER.name().nameWithMiddle()) .username(FAKER.internet().username()) .email(FAKER.internet().emailAddress()) .password(FAKER.internet().password()) .build(); } }
  12. Real Life Usage public final class UserDataFactory { private static

    final Faker FAKER = new Faker(); public static User passwordNotMatchMinRequirement() { return User.builder() .name(FAKER.name().nameWithMiddle()) .username(FAKER.internet().username()) .email(FAKER.internet().emailAddress()) .password(FAKER.internet().password(1,11)) .build(); } }
  13. How To Create Yours [private] Step 1 Create the YAML

    file with data Step 2 Create the Provider class Step 3 Create the Custom Faker
  14. How To Create Yours [private] Step 1 Create an yml

    file in the src/main/resources en: faker: java-conferences: names: - "Devoxx UK" - "DevNexus" - "Devoxx Belgium" - "JavaZone" - "Jfall" - "Jfokus" - "JavaLand" - "JNation" - "JPrime" - "JCON" en is the locale faker is the reference for the generation java-conferences is the provider key names is the list of values
  15. How To Create Yours [private] Step 2 Create the provider

    class public class JavaConferences extends AbstractProvider<BaseProviders> { protected JavaConferences(BaseProviders faker) { super(faker); faker.addPath(Locale.ENGLISH, Paths.get( getClass().getClassLoader().getResource("java-conferences.yml").getPath())); } public String name() { return resolve("java-conferences.names"); } } The constructor make the connection between the class and the YML file
  16. public class JavaConferences extends AbstractProvider<BaseProviders> { protected JavaConferences(BaseProviders faker) {

    super(faker); faker.addPath(Locale.ENGLISH, Paths.get( getClass().getClassLoader().getResource("java-conferences.yml").getPath())); } public String name() { return resolve("java-conferences.names"); } } How To Create Yours [private] Step 2 Create the provider class Then we use the provider key followed by the generators which will return one of the data present in the YML file
  17. How To Create Yours [private] Step 3 Create the custom

    Faker public class MyCustomFaker extends Faker { public JavaConferences javaConferences() { return getProvider(JavaConferences.class, JavaConferences::new, this); } }
  18. How To Create Yours [private] Usage Use the custom faker

    to generate your private data @Test @DisplayName("Should generate a Java event") void generateJavaEvent() { var javaEvent = myCustomFaker.javaConferences().name(); var country = myCustomFaker.country().name(); LOGGER.info(javaEvent); LOGGER.info(country); assertThat(javaEvent).isNotEmpty(); assertThat(country).isNotEmpty(); }
  19. How To Move Yours to DataFaker Step 1 Create the

    YAML file with data Step 2 Create the Provider class Step 3 Add the yaml file to the service file Step 4 Add it to the BaseProviders
  20. How To Create Yours [private] Step 1 Create an yml

    file in the src/main/resources en: faker: java-conferences: names: - "Devoxx UK" - "DevNexus" - "Devoxx Belgium" - "JavaZone" - "Jfall" - "Jfokus" - "JavaLand" - "JNation" - "JPrime" - "JCON" en is the locale faker is the reference for the generation java-conferences is the provider key names is the list of values
  21. How To Create Yours [private] Step 2 Create the provider

    class in the src/main/java/net.datafaker.providers.base public class JavaConferences extends AbstractProvider<BaseProviders> { protected JavaConferences(BaseProviders faker) { super(faker); } public String name() { return resolve("java-conferences.names"); } } The constructor make the connection between the class and the YML file
  22. How To Create Yours [private] Step 2 public class JavaConferences

    extends AbstractProvider<BaseProviders> { protected JavaConferences(BaseProviders faker) { super(faker); } public String name() { return resolve("java-conferences.names"); } } Then we use the provider key followed by the generators which will return one of the data present in the YML file Create the provider class in the src/main/java/net.datafaker.providers.base
  23. How To Create Yours [private] Step 3 Add the YML

    file name in the src/main/java/net.datafaker.service.files public class EnFile { // previous code ignored private static final List<EnFile> FILES = Stream.of( "address.yml", "Ancient.yml", "YOUR_FILE.yml", ).map(EnFile::new).toList();
  24. How To Create Yours [private] Step 4 Add the YML

    file name in the src/main/java/net.datafaker.providers.base public interface BaseProviders extends ProviderRegistration { default Address address() { return getProvider(Address.class, Address::new); } default JavaConferences javaConferences() { return getProvider(JavaConferences.class, JavaConferences::new); } }