### Basic Java Faker Usage Source: https://github.com/dius/java-faker/blob/master/README.md Instantiate the Faker class and use its methods to generate various types of fake data like names and addresses. No specific setup or imports are required beyond the dependency. ```java Faker faker = new Faker(); String name = faker.name().fullName(); // Miss Samanta Schmidt String firstName = faker.name().firstName(); // Emory String lastName = faker.name().lastName(); // Barton String streetAddress = faker.address().streetAddress(); // 60018 Sawayn Brooks Suite 449 ``` -------------------------------- ### Generate Phone Numbers with Java Faker Source: https://context7.com/dius/java-faker/llms.txt Generates locale-aware phone numbers, cell numbers, subscriber extensions, and line numbers. You can specify a locale to get numbers formatted for that region. ```java Faker faker = new Faker(); System.out.println(faker.phoneNumber().phoneNumber()); // "201-555-0123" System.out.println(faker.phoneNumber().cellPhone()); // "555-123-4567" System.out.println(faker.phoneNumber().extension()); // "4523" System.out.println(faker.phoneNumber().subscriberNumber()); // "8734" System.out.println(faker.phoneNumber().subscriberNumber(6)); // "873412" // With a French locale Faker frFaker = new Faker(new Locale("fr")); System.out.println(frFaker.phoneNumber().phoneNumber()); // "01 23 45 67 89" ``` -------------------------------- ### Package Project Source: https://github.com/dius/java-faker/wiki/Compile-into-a-JAR-file Navigate to the application directory within the container and execute the Maven package command to compile the project and create a JAR file. ```bash mvn package ``` -------------------------------- ### Faker Instantiation and Configuration Source: https://context7.com/dius/java-faker/llms.txt Demonstrates how to create instances of the Faker class with default settings, specific locales, seeded random number generators for reproducible results, or a combination of both. ```APIDOC ## Faker Instantiation and Configuration ### Description The `Faker` class is the main entry point for generating fake data. It can be constructed with default settings (English locale, random seed), a specific `java.util.Locale` for locale-appropriate data, or a seeded `java.util.Random` for deterministic and reproducible output, which is particularly useful for repeatable test suites. You can also combine locale and seed for fully controlled data generation. ### Methods - `Faker()`: Constructs a Faker instance with default locale (English) and a random seed. - `Faker(Locale locale)`: Constructs a Faker instance with the specified locale. - `Faker(Random random)`: Constructs a Faker instance with the specified seeded Random object. - `Faker(Locale locale, Random random)`: Constructs a Faker instance with the specified locale and seeded Random object. - `Faker.instance()`: Static factory method to get a Faker instance with default settings. - `Faker.instance(Locale locale)`: Static factory method to get a Faker instance with the specified locale. - `Faker.instance(Random random)`: Static factory method to get a Faker instance with the specified seeded Random object. - `Faker.instance(Locale locale, Random random)`: Static factory method to get a Faker instance with the specified locale and seeded Random object. ### Example Usage ```java import com.github.javafaker.Faker; import java.util.Locale; import java.util.Random; // Default (English locale, random seed) Faker faker = new Faker(); // Specific locale — generates locale-appropriate data (e.g., French names/addresses) Faker frFaker = new Faker(new Locale("fr")); // Seeded random — produces identical output every run (great for reproducible tests) Faker seededFaker = new Faker(new Random(12345L)); // Both locale and seed Faker deterministicFaker = new Faker(new Locale("de"), new Random(42L)); // Static factory methods Faker f1 = Faker.instance(); Faker f2 = Faker.instance(new Locale("es")); Faker f3 = Faker.instance(new Random(99L)); Faker f4 = Faker.instance(new Locale("pt_BR"), new Random(7L)); System.out.println(seededFaker.name().fullName()); // Always same output: e.g., "Horacio Heidenreich" ``` ``` -------------------------------- ### Instantiate Faker with Different Configurations Source: https://context7.com/dius/java-faker/llms.txt Demonstrates various ways to instantiate the Faker class, including default, specific locale, seeded random, and static factory methods. Using a seeded Random ensures reproducible output. ```java import com.github.javafaker.Faker; import java.util.Locale; import java.util.Random; // Default (English locale, random seed) Faker faker = new Faker(); // Specific locale — generates locale-appropriate data (e.g., French names/addresses) Faker frFaker = new Faker(new Locale("fr")); // Seeded random — produces identical output every run (great for reproducible tests) Faker seededFaker = new Faker(new Random(12345L)); // Both locale and seed Faker deterministicFaker = new Faker(new Locale("de"), new Random(42L)); // Static factory methods Faker f1 = Faker.instance(); Faker f2 = Faker.instance(new Locale("es")); Faker f3 = Faker.instance(new Random(99L)); Faker f4 = Faker.instance(new Locale("pt_BR"), new Random(7L)); System.out.println(seededFaker.name().fullName()); // Always same output: e.g., "Horacio Heidenreich" ``` -------------------------------- ### Clone Repository Source: https://github.com/dius/java-faker/wiki/Compile-into-a-JAR-file Clone the project repository to your local machine. ```bash git clone ... && cd ... ``` -------------------------------- ### Run Maven Container Source: https://github.com/dius/java-faker/wiki/Compile-into-a-JAR-file Execute the Maven build process inside a Docker or Podman container. This command mounts the current directory into the container and runs the Maven package command. ```bash podman run -it --rm -v $(pwd):/app docker.io/library/maven /bin/bash ``` -------------------------------- ### faker.lorem() — Placeholder Text Source: https://context7.com/dius/java-faker/llms.txt Generates lorem ipsum-style placeholder words, sentences, paragraphs, and character strings of configurable length. ```APIDOC ## faker.lorem() ### Description Generates lorem ipsum-style placeholder words, sentences, paragraphs, and character strings of configurable length. ### Methods - `word()`: Generates a single placeholder word. - `words(int)`: Generates a list of placeholder words. - `sentence()`: Generates a placeholder sentence. - `sentence(int)`: Generates a placeholder sentence with approximately a specified number of words. - `sentence(int, int)`: Generates a placeholder sentence with an exact number of words. - `sentences(int)`: Generates a list of placeholder sentences. - `paragraph()`: Generates a placeholder paragraph. - `paragraph(int)`: Generates a placeholder paragraph with approximately a specified number of sentences. - `paragraphs(int)`: Generates a list of placeholder paragraphs. - `fixedString(int)`: Generates a string of a fixed length. - `characters(int)`: Generates a string of random characters with a specified length. - `characters(int, int)`: Generates a string of random characters with a length between two specified values. - `characters(int, boolean)`: Generates a string of random characters with a specified length, including mixed case if true. - `characters(int, boolean, boolean)`: Generates a string of random characters with a specified length, including mixed case and digits if true. - `character()`: Generates a single random character. ``` -------------------------------- ### faker.internet() Source: https://context7.com/dius/java-faker/llms.txt Generates email addresses, domain names, URLs, IP addresses, MAC addresses, passwords, slugs, UUIDs, and user agents. ```APIDOC ## faker.internet() ### Description Generates email addresses, domain names, URLs, IP addresses, MAC addresses, passwords, slugs, UUIDs, and user agents. ### Methods - `emailAddress()`: Generates a random email address. - `emailAddress(String localPart)`: Generates an email address with a specified local part. - `safeEmailAddress()`: Generates a safe email address (e.g., example.com). - `domainName()`: Generates a random domain name. - `domainWord()`: Generates a random domain word. - `domainSuffix()`: Generates a random domain suffix. - `url()`: Generates a random URL. - `ipV4Address()`: Generates a random IPv4 address. - `privateIpV4Address()`: Generates a random private IPv4 address. - `publicIpV4Address()`: Generates a random public IPv4 address. - `ipV4Cidr()`: Generates a random IPv4 CIDR notation. - `ipV6Address()`: Generates a random IPv6 address. - `ipV6Cidr()`: Generates a random IPv6 CIDR notation. - `macAddress()`: Generates a random MAC address. - `macAddress(String prefix)`: Generates a MAC address with a specified prefix. - `password()`: Generates a random password. - `password(int minLength, int maxLength)`: Generates a password with a specified length range. - `password(int minLength, int maxLength, boolean includeUppercase)`: Generates a password with specified length and includes uppercase letters. - `password(int minLength, int maxLength, boolean includeUppercase, boolean includeSpecialChars)`: Generates a password with specified length, uppercase letters, and special characters. - `password(boolean includeDigits)`: Generates a password that includes digits. - `uuid()`: Generates a random UUID. - `slug()`: Generates a random slug. - `userAgent(Internet.UserAgent browser)`: Generates a user agent string for a specific browser. - `userAgentAny()`: Generates a random user agent string for any browser. - `avatar()`: Generates a URL to an avatar image. - `image()`: Generates a URL to a placeholder image. - `image(int width, int height, boolean gray, String text)`: Generates a custom placeholder image URL. ### Example ```java Faker faker = new Faker(); // Emails System.out.println(faker.internet().emailAddress()); System.out.println(faker.internet().emailAddress("john.doe")); System.out.println(faker.internet().safeEmailAddress()); // Domains and URLs System.out.println(faker.internet().domainName()); System.out.println(faker.internet().domainWord()); System.out.println(faker.internet().domainSuffix()); System.out.println(faker.internet().url()); // Network System.out.println(faker.internet().ipV4Address()); System.out.println(faker.internet().privateIpV4Address()); System.out.println(faker.internet().publicIpV4Address()); System.out.println(faker.internet().ipV4Cidr()); System.out.println(faker.internet().ipV6Address()); System.out.println(faker.internet().ipV6Cidr()); System.out.println(faker.internet().macAddress()); System.out.println(faker.internet().macAddress("fa:16")); // Passwords System.out.println(faker.internet().password()); System.out.println(faker.internet().password(12, 20)); System.out.println(faker.internet().password(8, 16, true)); System.out.println(faker.internet().password(8, 16, true, true)); System.out.println(faker.internet().password(true)); // Other System.out.println(faker.internet().uuid()); System.out.println(faker.internet().slug()); System.out.println(faker.internet().userAgent(Internet.UserAgent.CHROME)); System.out.println(faker.internet().userAgentAny()); System.out.println(faker.internet().avatar()); System.out.println(faker.internet().image()); System.out.println(faker.internet().image(640, 480, false, "hello")); ``` ``` -------------------------------- ### Generate E-Commerce Data with faker.commerce() Source: https://context7.com/dius/java-faker/llms.txt Use faker.commerce() to generate product names, department names, prices, colors, materials, and promotional codes for e-commerce scenarios. Prices can be generated as a default string or within a specified range. ```java Faker faker = new Faker(); System.out.println(faker.commerce().productName()); // "Practical Granite Chair" System.out.println(faker.commerce().department()); // "Electronics & Sports" System.out.println(faker.commerce().price()); // "49.99" System.out.println(faker.commerce().price(5.0, 500.0)); // "234.50" System.out.println(faker.commerce().color()); // "cyan" System.out.println(faker.commerce().material()); // "Granite" System.out.println(faker.commerce().promotionCode()); // "SavingsDay592837" System.out.println(faker.commerce().promotionCode(4)); // "HappyFlash9274" ``` -------------------------------- ### Generate Address Data with Java Faker Source: https://context7.com/dius/java-faker/llms.txt Illustrates how to generate postal and geographic data, including street names, addresses, zip codes, cities, states, countries, coordinates, and time zones using the faker.address() object. Supports state-specific zip codes and full address generation. ```java Faker faker = new Faker(); System.out.println(faker.address().streetName()); // "Buckridge Road" System.out.println(faker.address().streetAddress()); // "60018 Sawayn Brooks Suite 449" System.out.println(faker.address().streetAddress(true)); // includes secondary address System.out.println(faker.address().secondaryAddress()); // "Apt. 123" System.out.println(faker.address().zipCode()); // "10013" System.out.println(faker.address().zipCodeByState("NY")); // "10001" (state-specific) System.out.println(faker.address().city()); // "Port Jermaine" System.out.println(faker.address().state()); // "California" System.out.println(faker.address().stateAbbr()); // "CA" System.out.println(faker.address().country()); // "Germany" System.out.println(faker.address().countryCode()); // "DE" System.out.println(faker.address().latitude()); // "45.12345678" System.out.println(faker.address().longitude()); // "-73.98765432" System.out.println(faker.address().timeZone()); // "America/New_York" System.out.println(faker.address().fullAddress()); // "123 Main St, Springfield, IL 62701" System.out.println(faker.address().buildingNumber()); // "42" ``` -------------------------------- ### Generate Person Names with Java Faker Source: https://context7.com/dius/java-faker/llms.txt Shows how to generate various types of personal names, including full names, first/last names, prefixes, suffixes, titles, usernames, and blood groups using the faker.name() object. ```java Faker faker = new Faker(); System.out.println(faker.name().fullName()); // "Miss Samanta Schmidt" System.out.println(faker.name().name()); // "James Jones Jr." System.out.println(faker.name().nameWithMiddle()); // "Mrs. Ella Geraldine Fitzgerald" System.out.println(faker.name().firstName()); // "Emory" System.out.println(faker.name().lastName()); // "Barton" System.out.println(faker.name().prefix()); // "Mr." System.out.println(faker.name().suffix()); // "PhD" System.out.println(faker.name().title()); // "Lead Solutions Specialist" System.out.println(faker.name().username()); // "jim.jones" System.out.println(faker.name().bloodGroup()); // "AB+" ``` -------------------------------- ### faker.options() — Random Selection Utilities Source: https://context7.com/dius/java-faker/llms.txt Picks a random element from a varargs list, an array, a `List`, or an `Enum`. ```APIDOC ## faker.options() ### Description Picks a random element from a varargs list, an array, a `List`, or an `Enum`. ### Methods - `option(T... values)`: Picks a random element from a varargs list of any type `T`. - `option(Class clazz)`: Picks a random element from an `Enum` of type `T`. - `nextElement(T[] array)`: Picks a random element from an array of any type `T`. - `nextElement(List list)`: Picks a random element from a `List` of any type `T`. ``` -------------------------------- ### Configure Locale-Specific Data Generation Source: https://context7.com/dius/java-faker/llms.txt Enable locale-aware data generation by passing a `java.util.Locale` object to the `Faker` constructor. This ensures culturally appropriate output for names, addresses, etc. If a translation is missing for a specific field in the chosen locale, it will automatically fall back to English. ```java import java.util.Locale; // German Faker de = new Faker(new Locale("de")); System.out.println(de.name().fullName()); // "Hans Müller" System.out.println(de.address().city()); // "München" // Japanese Faker ja = new Faker(new Locale("ja")); System.out.println(ja.name().fullName()); // Japanese name // Brazilian Portuguese Faker ptBR = new Faker(new Locale("pt_BR")); System.out.println(ptBR.name().firstName()); // "João" System.out.println(ptBR.address().state()); // "São Paulo" // Swedish — with locale-specific ID numbers Faker sv = new Faker(new Locale("sv_SE")); System.out.println(sv.idNumber().validSvSeSsn()); // "850101-0000" // Locale fallback: if a field has no sv translation, English is used automatically Faker svFaker = new Faker(new Locale("sv")); System.out.println(svFaker.lorem().word()); // Falls back to English lorem ``` -------------------------------- ### Pattern-Based String Generation Source: https://context7.com/dius/java-faker/llms.txt Generate strings using patterns with faker.numerify(), faker.letterify(), faker.bothify(), and faker.regexify(). '#' generates digits, '?' generates letters, and regexify matches a full regex. ```java Faker faker = new Faker(); // '#' → random digit 0–9 System.out.println(faker.numerify("ORDER-###-####")); // "ORDER-592-8374" System.out.println(faker.numerify("(###) ###-####")); // "(415) 782-9034" // '?' → random lowercase letter System.out.println(faker.letterify("??-?????")); // "xk-abcde" System.out.println(faker.letterify("TICKET-???", true)); // "TICKET-XKQ" (uppercase) // Combined '#' and '?' System.out.println(faker.bothify("SKU-??##-??##")); // "SKU-ab47-xq19" System.out.println(faker.bothify("PASS-??##??", true)); // "PASS-AB74XQ" (upper) // Full regex matching (uses Generex library) System.out.println(faker.regexify("[A-Z]{3}-\\d{4}")); // "XKQ-5823" System.out.println(faker.regexify("(foo|bar|baz)\\d{2}")); // "bar47" System.out.println(faker.regexify("[0-9a-f]{8}-[0-9a-f]{4}")); // UUID-like hex segment ``` -------------------------------- ### faker.numerify() / faker.letterify() / faker.bothify() / faker.regexify() Source: https://context7.com/dius/java-faker/llms.txt Utility methods to generate strings by replacing pattern characters with random digits, letters, or matching a full regex. ```APIDOC ## faker.numerify() / faker.letterify() / faker.bothify() / faker.regexify() ### Description Utility methods on `Faker` itself to generate strings by replacing pattern characters with random digits (`#`), letters (`?`), or both, as well as generating strings matching a full regex. ### Methods - `numerify(String value)`: Replaces '#' with random digits (0-9). - `letterify(String value)`: Replaces '?' with random lowercase letters. - `letterify(String value, boolean uppercase)`: Replaces '?' with random letters, optionally uppercase. - `bothify(String value)`: Replaces '#' with digits and '?' with lowercase letters. - `bothify(String value, boolean uppercase)`: Replaces '#' with digits and '?' with letters, optionally uppercase. - `regexify(String regex)`: Generates a string that matches the provided regular expression. ``` -------------------------------- ### Generate Business Data with faker.company() Source: https://context7.com/dius/java-faker/llms.txt Utilize faker.company() to generate business-related data such as company names, suffixes, industries, professions, buzzwords, catch phrases, logos, and website URLs. ```java Faker faker = new Faker(); System.out.println(faker.company().name()); // "Acme Corp LLC" System.out.println(faker.company().suffix()); // "Inc" System.out.println(faker.company().industry()); // "Information Technology & Services" System.out.println(faker.company().profession()); // "nurse" System.out.println(faker.company().buzzword()); // "synergize" System.out.println(faker.company().catchPhrase()); // "Networked 24/7 architecture" System.out.println(faker.company().bs()); // "leverage agile frameworks" System.out.println(faker.company().logo()); // "https://pigment.github.io/fake-logos/logos/medium/color/3.png" System.out.println(faker.company().url()); // "www.acmecorp.com" ``` -------------------------------- ### Random Selection Utilities with faker.options() Source: https://context7.com/dius/java-faker/llms.txt Use faker.options() to pick random elements from varargs, arrays, Lists, or Enums. The option() method is used for varargs and Enums, while nextElement() is for arrays and Lists. ```java import java.util.Arrays; import java.util.List; Faker faker = new Faker(); // Pick from varargs String color = faker.options().option("red", "green", "blue", "yellow"); // "green" int size = faker.options().option(1, 2, 4, 8, 16); // 4 // Pick from Enum enum Status { ACTIVE, INACTIVE, PENDING } Status s = faker.options().option(Status.class); // Status.PENDING // Pick from array String[] regions = {"North", "South", "East", "West"}; String region = faker.options().nextElement(regions); // "East" // Pick from List List tiers = Arrays.asList("bronze", "silver", "gold", "platinum"); String tier = faker.options().nextElement(tiers); // "silver" ``` -------------------------------- ### Generate Lorem Ipsum Placeholder Text with Java Faker Source: https://context7.com/dius/java-faker/llms.txt Use faker.lorem() to generate words, sentences, paragraphs, and character strings. You can specify the number of words, sentences, or characters, and control case and digit inclusion for character strings. ```java Faker faker = new Faker(); System.out.println(faker.lorem().word()); // "ipsum" System.out.println(faker.lorem().words(5)); // [lorem, ipsum, dolor, sit, amet] System.out.println(faker.lorem().sentence()); // "Lorem ipsum dolor sit amet." System.out.println(faker.lorem().sentence(5)); // sentence with ~5 words System.out.println(faker.lorem().sentence(5, 0)); // exactly 5 words System.out.println(faker.lorem().sentences(3)); // list of 3 sentences System.out.println(faker.lorem().paragraph()); // multi-sentence paragraph System.out.println(faker.lorem().paragraph(5)); // paragraph with ~5 sentences System.out.println(faker.lorem().paragraphs(3)); // list of 3 paragraphs System.out.println(faker.lorem().fixedString(100)); // exactly 100 characters System.out.println(faker.lorem().characters(10)); // "abcdefghij" System.out.println(faker.lorem().characters(5, 10)); // 5–10 random chars System.out.println(faker.lorem().characters(8, true)); // mixed case System.out.println(faker.lorem().characters(8, true, true)); // mixed case + digits System.out.println(faker.lorem().character()); // 'x' ``` -------------------------------- ### Generate Random Dates and Times with Java Faker Source: https://context7.com/dius/java-faker/llms.txt Utilize faker.date() to create future and past dates, birthdays, and date ranges. Requires importing java.util.Date and java.util.concurrent.TimeUnit. ```java import java.util.concurrent.TimeUnit; import java.util.Date; Faker faker = new Faker(); // Future dates Date withinOneYear = faker.date().future(365, TimeUnit.DAYS); Date inAtLeast30d = faker.date().future(60, 30, TimeUnit.DAYS); // 30–60 days out Date futureFromRef = faker.date().future(10, TimeUnit.HOURS, someReferenceDate); // Past dates Date pastWeek = faker.date().past(7, TimeUnit.DAYS); Date pastWithMin = faker.date().past(30, 7, TimeUnit.DAYS); // 7–30 days ago Date pastFromRef = faker.date().past(5, TimeUnit.DAYS, someReferenceDate); // Range Date from = new Date(0); // epoch Date to = new Date(); // now Date between = faker.date().between(from, to); // Birthdays Date birthday = faker.date().birthday(); // age 18–65 Date youngBirthday = faker.date().birthday(18, 30); // age 18–30 ``` -------------------------------- ### Generate Internet and Network Data with Java Faker Source: https://context7.com/dius/java-faker/llms.txt Generates email addresses, domain names, URLs, IP addresses, MAC addresses, passwords, slugs, UUIDs, and user agents. Specify a username for email addresses or a prefix for MAC addresses. ```java Faker faker = new Faker(); // Emails System.out.println(faker.internet().emailAddress()); // "jim.jones@gmail.com" System.out.println(faker.internet().emailAddress("john.doe")); // "john.doe@yahoo.com" System.out.println(faker.internet().safeEmailAddress()); // "jim.jones@example.com" // Domains and URLs System.out.println(faker.internet().domainName()); // "jones.com" System.out.println(faker.internet().domainWord()); // "jones" System.out.println(faker.internet().domainSuffix()); // "net" System.out.println(faker.internet().url()); // "www.john-jones.org" // Network System.out.println(faker.internet().ipV4Address()); // "192.168.1.45" System.out.println(faker.internet().privateIpV4Address()); // "10.0.0.1" System.out.println(faker.internet().publicIpV4Address()); // "203.0.113.42" System.out.println(faker.internet().ipV4Cidr()); // "192.168.0.0/24" System.out.println(faker.internet().ipV6Address()); // "2001:0db8:85a3:0000:0000:8a2e:0370:7334" System.out.println(faker.internet().ipV6Cidr()); // "2001:db8::/32" System.out.println(faker.internet().macAddress()); // "e1:d2:a3:b4:c5:f6" System.out.println(faker.internet().macAddress("fa:16")); // "fa:16:3e:2a:1b:0c" // Passwords System.out.println(faker.internet().password()); // "aB3kf8jq" System.out.println(faker.internet().password(12, 20)); // length 12-20 System.out.println(faker.internet().password(8, 16, true)); // with uppercase System.out.println(faker.internet().password(8, 16, true, true)); // with uppercase + special chars System.out.println(faker.internet().password(true)); // include digits // Other System.out.println(faker.internet().uuid()); // "550e8400-e29b-41d4-a716-446655440000" System.out.println(faker.internet().slug()); // "lorem_ipsum" System.out.println(faker.internet().userAgent(Internet.UserAgent.CHROME)); // Chrome UA string System.out.println(faker.internet().userAgentAny()); // random browser UA string System.out.println(faker.internet().avatar()); // URL to avatar image System.out.println(faker.internet().image()); // URL to placeholder image System.out.println(faker.internet().image(640, 480, false, "hello")); // custom lorempixel URL ``` -------------------------------- ### faker.commerce() — E-Commerce Data Source: https://context7.com/dius/java-faker/llms.txt Generates product names, department names, prices, colors, materials, and promotional codes for e-commerce use cases. ```APIDOC ## faker.commerce() ### Description Generates product names, department names, prices, colors, materials, and promotional codes for e-commerce use cases. ### Methods - `productName()`: Generates a fake product name. - `department()`: Generates a fake department name. - `price()`: Generates a random price. - `price(double min, double max)`: Generates a random price within a specified range. - `color()`: Generates a fake color name. - `material()`: Generates a fake material name. - `promotionCode()`: Generates a fake promotion code. - `promotionCode(int length)`: Generates a fake promotion code of a specified length. ``` -------------------------------- ### Generate Financial Data with Java Faker Source: https://context7.com/dius/java-faker/llms.txt Use faker.finance() to generate Luhn-compliant credit card numbers, IBANs for various countries, and BIC codes. Ensure CreditCardType is imported if specifying card types. ```java Faker faker = new Faker(); // Credit cards (all pass Luhn check) System.out.println(faker.finance().creditCard()); // "4532-1234-5678-9012" System.out.println(faker.finance().creditCard(CreditCardType.VISA)); // Visa card number System.out.println(faker.finance().creditCard(CreditCardType.MASTERCARD)); // IBAN (International Bank Account Number) System.out.println(faker.finance().iban()); // random country: "DE89370400440532013000" System.out.println(faker.finance().iban("DE")); // German IBAN System.out.println(faker.finance().iban("GB")); // UK IBAN: "GB29NWBK60161331926819" // BIC (Bank Identifier Code) System.out.println(faker.finance().bic()); // "DEUTDEDB" ``` -------------------------------- ### Seeded Reproducible Output with Java Faker Source: https://context7.com/dius/java-faker/llms.txt Using a seeded Random ensures deterministic output, which is essential for unit and snapshot testing. The same seed will always produce the same sequence of fake data. ```java import java.util.Random; Random seed = new Random(9876L); Faker faker = new Faker(seed); // These values will always be the same for seed 9876L String name = faker.name().fullName(); String email = faker.internet().emailAddress(); String address = faker.address().fullAddress(); int orderId = faker.number().numberBetween(1000, 9999); System.out.println(name + " | " + email + " | " + orderId); // Always prints the same line ``` ```java // Use in a test @Test public void testUserDataIsStable() { Faker faker = new Faker(new Random(42L)); assertEquals("Jammie Zboncak", faker.name().fullName()); assertEquals("jammie.zboncak@hotmail.com", faker.internet().emailAddress()); } ``` -------------------------------- ### faker.name() — Person Names Source: https://context7.com/dius/java-faker/llms.txt Provides methods to generate various components of personal names, including full names, first names, last names, prefixes, suffixes, titles, usernames, and blood groups. ```APIDOC ## faker.name() — Person Names ### Description Generates realistic personal names including full names, first/last names, prefixes, suffixes, titles, usernames, and blood groups. Each method returns a different type of name component. ### Methods - `fullName()`: Generates a complete name, potentially including a prefix and suffix. - `name()`: Generates a name, possibly including a suffix like Jr. or III. - `nameWithMiddle()`: Generates a name that includes a middle name. - `firstName()`: Generates a first name. - `lastName()`: Generates a last name. - `prefix()`: Generates a title prefix (e.g., Mr., Mrs., Dr.). - `suffix()`: Generates a suffix (e.g., PhD, Jr., Sr.). - `title()`: Generates a job title. - `username()`: Generates a username. - `bloodGroup()`: Generates a blood group (e.g., A+, O-). ### Example Usage ```java import com.github.javafaker.Faker; Faker faker = new Faker(); System.out.println(faker.name().fullName()); // "Miss Samanta Schmidt" System.out.println(faker.name().name()); // "James Jones Jr." System.out.println(faker.name().nameWithMiddle()); // "Mrs. Ella Geraldine Fitzgerald" System.out.println(faker.name().firstName()); // "Emory" System.out.println(faker.name().lastName()); // "Barton" System.out.println(faker.name().prefix()); // "Mr." System.out.println(faker.name().suffix()); // "PhD" System.out.println(faker.name().title()); // "Lead Solutions Specialist" System.out.println(faker.name().username()); // "jim.jones" System.out.println(faker.name().bloodGroup()); // "AB+" ``` ``` -------------------------------- ### Evaluate YAML Expressions with faker.expression() Source: https://context7.com/dius/java-faker/llms.txt Use `faker.expression()` to evaluate inline expressions from YAML data files. Supports recursive templates and method invocations with arguments. Ensure the Faker instance is initialized. ```java Faker faker = new Faker(); // Reference a named faker method System.out.println(faker.expression("#{Name.first_name} #{Name.last_name}")); // e.g., "John Smith" // Compose address System.out.println(faker.expression("#{Address.street_address}, #{Address.city}, #{Address.state_abbr}")); // e.g., "123 Elm St, Springfield, IL" // Built-in helpers with arguments System.out.println(faker.expression("#{regexify '(red|blue|green)'}")); // "blue" System.out.println(faker.expression("#{bothify '????-##'}")); // "xkqb-74" System.out.println(faker.expression("#{number.number_between '100','999'}")); // "542" System.out.println(faker.expression("#{Internet.email_address}")); // "john.smith@gmail.com" ``` -------------------------------- ### faker.number() Source: https://context7.com/dius/java-faker/llms.txt Generates random integers, longs, doubles, digit strings, and constrained numeric ranges. ```APIDOC ## faker.number() ### Description Generates random integers, longs, doubles, digit strings, and constrained numeric ranges. ### Methods - `randomDigit()`: Generates a random digit (0-9). - `randomDigitNotZero()`: Generates a random digit not equal to zero (1-9). - `numberBetween(int min, int max)`: Generates a random integer between min (inclusive) and max (exclusive). - `numberBetween(long min, long max)`: Generates a random long between min (inclusive) and max (exclusive). - `randomNumber()`: Generates a random long number. - `randomNumber(int numDigits, boolean strict)`: Generates a random number with a specific number of digits, with an option for strict length. - `randomDouble(int scale, double low, double high)`: Generates a random double with a specified scale and range. - `digits(int numDigits)`: Generates a string of random digits with a specified length. - `digit()`: Generates a single random digit as a string. ### Example ```java Faker faker = new Faker(); System.out.println(faker.number().randomDigit()); System.out.println(faker.number().randomDigitNotZero()); System.out.println(faker.number().numberBetween(1, 100)); System.out.println(faker.number().numberBetween(1L, 1000000L)); System.out.println(faker.number().randomNumber()); System.out.println(faker.number().randomNumber(5, true)); System.out.println(faker.number().randomDouble(2, 10, 100)); System.out.println(faker.number().digits(8)); System.out.println(faker.number().digit()); ``` ``` -------------------------------- ### Generate Identity Numbers with faker.idNumber() Source: https://context7.com/dius/java-faker/llms.txt Generate valid and invalid Social Security Numbers (SSN) and locale-specific identity numbers. For Swedish IDs, instantiate Faker with the appropriate locale. ```java Faker faker = new Faker(); System.out.println(faker.idNumber().valid()); // "123-45-6789" (valid SSN format) System.out.println(faker.idNumber().invalid()); // "987-65-0000" (invalid SSN format) System.out.println(faker.idNumber().ssnValid()); // valid US SSN passing checksum rules // Swedish Personal Identity Number Faker svFaker = new Faker(new Locale("sv_SE")); System.out.println(svFaker.idNumber().validSvSeSsn()); // valid Swedish personnummer System.out.println(svFaker.idNumber().invalidSvSeSsn()); // invalid Swedish personnummer ``` -------------------------------- ### Maven Dependency for Java Faker Source: https://github.com/dius/java-faker/blob/master/README.md Add this XML stanza to your pom.xml file within the section to include the Java Faker library. ```xml com.github.javafaker javafaker 1.0.2 ``` -------------------------------- ### faker.date() — Dates and Times Source: https://context7.com/dius/java-faker/llms.txt Generates random past/future dates, birthday dates, and date ranges using `java.util.Date` and `java.util.concurrent.TimeUnit`. ```APIDOC ## faker.date() ### Description Generates random past/future dates, birthday dates, and date ranges using `java.util.Date` and `java.util.concurrent.TimeUnit`. ### Methods - `future(int, TimeUnit)`: Generates a future date within the specified duration. - `future(int, int, TimeUnit)`: Generates a future date within a specified range. - `future(int, TimeUnit, Date)`: Generates a future date relative to a reference date. - `past(int, TimeUnit)`: Generates a past date within the specified duration. - `past(int, int, TimeUnit)`: Generates a past date within a specified range. - `past(int, TimeUnit, Date)`: Generates a past date relative to a reference date. - `between(Date, Date)`: Generates a date between two specified dates. - `birthday()`: Generates a random birthday date (age 18-65). - `birthday(int, int)`: Generates a random birthday date within a specified age range. ``` -------------------------------- ### faker.phoneNumber() Source: https://context7.com/dius/java-faker/llms.txt Generates locale-aware phone numbers, cell numbers, subscriber extensions, and line numbers. ```APIDOC ## faker.phoneNumber() ### Description Generates locale-aware phone numbers, cell numbers, subscriber extensions, and line numbers. ### Methods - `phoneNumber()`: Generates a locale-aware phone number. - `cellPhone()`: Generates a locale-aware cell phone number. - `extension()`: Generates a random extension number. - `subscriberNumber()`: Generates a random subscriber number. - `subscriberNumber(int length)`: Generates a subscriber number of a specific length. ### Example ```java Faker faker = new Faker(); System.out.println(faker.phoneNumber().phoneNumber()); System.out.println(faker.phoneNumber().cellPhone()); System.out.println(faker.phoneNumber().extension()); System.out.println(faker.phoneNumber().subscriberNumber()); System.out.println(faker.phoneNumber().subscriberNumber(6)); // With a French locale Faker frFaker = new Faker(new Locale("fr")); System.out.println(frFaker.phoneNumber().phoneNumber()); ``` ``` -------------------------------- ### Generate Themed Pop Culture Data Source: https://context7.com/dius/java-faker/llms.txt Java Faker provides themed generators for various categories like Fantasy, Sci-Fi, TV & Film, Games, Science, Sports, and more. Instantiate Faker and call the relevant themed generator methods. ```java Faker faker = new Faker(); // Fantasy / Sci-Fi System.out.println(faker.harryPotter().character()); // "Harry Potter" System.out.println(faker.harryPotter().location()); // "Hogwarts" System.out.println(faker.harryPotter().spell()); // "Expelliarmus" System.out.println(faker.lordOfTheRings().character()); // "Frodo Baggins" System.out.println(faker.gameOfThrones().character()); // "Jon Snow" System.out.println(faker.dune().character()); // "Paul Atreides" // TV & Film System.out.println(faker.friends().character()); // "Chandler Bing" System.out.println(faker.rickAndMorty().character()); // "Rick Sanchez" System.out.println(faker.twinPeaks().character()); // "Dale Cooper" // Games & Esports System.out.println(faker.leagueOfLegends().champion()); // "Ahri" System.out.println(faker.overwatch().hero()); // "Tracer" System.out.println(faker.starCraft().unit()); // "Marine" System.out.println(faker.zelda().game()); // "Ocarina of Time" // Science & Space System.out.println(faker.space().planet()); // "Mars" System.out.println(faker.space().galaxy()); // "Andromeda" System.out.println(faker.space().nebula()); // "Orion Nebula" System.out.println(faker.hitchhikersGuideToTheGalaxy().character()); // "Arthur Dent" // Sports System.out.println(faker.basketball().players()); // "LeBron James" System.out.println(faker.esports().team()); // "Team Liquid" // Other System.out.println(faker.beer().name()); // "Duvel" System.out.println(faker.food().dish()); // "Chicken Tikka Masala" System.out.println(faker.music().instrument()); // "Piano" System.out.println(faker.programmingLanguage().name()); // "Kotlin" System.out.println(faker.weather().description()); // "Partly cloudy" System.out.println(faker.medical().diseaseName()); // "Influenza" System.out.println(faker.animal().name()); // "Lion" System.out.println(faker.nation().flag()); // "🇺🇸" System.out.println(faker.crypto().md5()); // "d41d8cd98f00b204e9800998ecf8427e" ``` -------------------------------- ### Generate Industry Codes and Identifiers with Java Faker Source: https://context7.com/dius/java-faker/llms.txt Employ faker.code() to generate valid ISBN-10, ISBN-13, EAN-8, EAN-13, GTIN, IMEI, and ASIN codes, all with proper check digits. ```java Faker faker = new Faker(); // ISBN System.out.println(faker.code().isbn10()); // "9604250590" System.out.println(faker.code().isbn10(true)); // "960-425-059-0" (with separators) System.out.println(faker.code().isbn13()); // "9789604250590" System.out.println(faker.code().isbn13(true)); // "978-960-425-059-0" // Barcodes System.out.println(faker.code().ean8()); // "12345670" (EAN-8, valid check digit) System.out.println(faker.code().ean13()); // "1234567890128" (EAN-13) System.out.println(faker.code().gtin8()); // "12345670" System.out.println(faker.code().gtin13()); // "1234567890128" // Device and retail IDs System.out.println(faker.code().imei()); // "490154203237518" (valid Luhn) System.out.println(faker.code().asin()); // "B00ABC123D" ``` -------------------------------- ### Gradle Dependency for Java Faker Source: https://github.com/dius/java-faker/blob/master/README.md Include this line in your build.gradle file under the dependencies block to add the Java Faker library to your project. ```groovy dependencies { implementation 'com.github.javafaker:javafaker:1.0.2' } ``` -------------------------------- ### faker.address() — Postal and Geographic Data Source: https://context7.com/dius/java-faker/llms.txt Provides methods to generate various components of postal and geographic data, including street addresses, cities, states, zip codes, countries, coordinates, and time zones. ```APIDOC ## faker.address() — Postal and Geographic Data ### Description Generates realistic postal and geographic data, including street addresses, cities, states, zip codes, countries, coordinates, and time zones. It supports generating full addresses as well as individual components. ### Methods - `streetName()`: Generates a street name. - `streetAddress()`: Generates a full street address, including number and street name. - `streetAddress(boolean includeSecondary)`: Generates a full street address, optionally including a secondary address. - `secondaryAddress()`: Generates a secondary address component (e.g., Apt. 123). - `zipCode()`: Generates a postal code (zip code). - `zipCodeByState(String state)`: Generates a postal code specific to the given state. - `city()`: Generates a city name. - `state()`: Generates a state name. - `stateAbbr()`: Generates a state abbreviation. - `country()`: Generates a country name. - `countryCode()`: Generates a country code. - `latitude()`: Generates a random latitude coordinate. - `longitude()`: Generates a random longitude coordinate. - `timeZone()`: Generates a time zone identifier. - `fullAddress()`: Generates a complete address string, including city, state, and zip code. - `buildingNumber()`: Generates a building number. ### Example Usage ```java import com.github.javafaker.Faker; Faker faker = new Faker(); System.out.println(faker.address().streetName()); // "Buckridge Road" System.out.println(faker.address().streetAddress()); // "60018 Sawayn Brooks Suite 449" System.out.println(faker.address().streetAddress(true)); // includes secondary address System.out.println(faker.address().secondaryAddress()); // "Apt. 123" System.out.println(faker.address().zipCode()); // "10013" System.out.println(faker.address().zipCodeByState("NY")); // "10001" (state-specific) System.out.println(faker.address().city()); // "Port Jermaine" System.out.println(faker.address().state()); // "California" System.out.println(faker.address().stateAbbr()); // "CA" System.out.println(faker.address().country()); // "Germany" System.out.println(faker.address().countryCode()); // "DE" System.out.println(faker.address().latitude()); // "45.12345678" System.out.println(faker.address().longitude()); // "-73.98765432" System.out.println(faker.address().timeZone()); // "America/New_York" System.out.println(faker.address().fullAddress()); // "123 Main St, Springfield, IL 62701" System.out.println(faker.address().buildingNumber()); // "42" ``` ```