datafaker
Generating fake data for the JVM (Java, Kotlin, Groovy) has never been easier!
Top Related Projects
:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Brings the popular ruby faker gem to Java
A library for generating fake data such as names, addresses, and phone numbers.
Quick Overview
Datafaker is a Java library that generates fake data for testing and development purposes. It provides a wide range of data types and customizable options, allowing developers to create realistic and diverse datasets quickly and easily.
Pros
- Extensive variety of data types and generators
- Highly customizable and flexible
- Easy to integrate with existing Java projects
- Active development and community support
Cons
- Limited documentation for advanced use cases
- Some generators may produce repetitive data in large datasets
- Performance can be slower for very large data generation tasks
- Primarily focused on Java, with limited support for other languages
Code Examples
Generate a random name:
Faker faker = new Faker();
String name = faker.name().fullName();
System.out.println(name);
Generate a list of 5 random email addresses:
Faker faker = new Faker();
List<String> emails = faker.collection(
() -> faker.internet().emailAddress()
).len(5).generate();
emails.forEach(System.out::println);
Generate a custom object with fake data:
Faker faker = new Faker();
Person person = faker.expression("#{create('com.example.Person', 'name', '#{name.fullName}', 'age', '#{number.numberBetween '18','80'}')}");
System.out.println(person);
Getting Started
To use Datafaker in your Java project, add the following dependency to your Maven pom.xml
file:
<dependency>
<groupId>net.datafaker</groupId>
<artifactId>datafaker</artifactId>
<version>1.8.1</version>
</dependency>
For Gradle, add this to your build.gradle
file:
implementation 'net.datafaker:datafaker:1.8.1'
Then, you can start using Datafaker in your Java code:
import net.datafaker.Faker;
public class Example {
public static void main(String[] args) {
Faker faker = new Faker();
System.out.println(faker.name().fullName());
System.out.println(faker.address().cityName());
System.out.println(faker.date().birthday());
}
}
This will generate and print a random full name, city name, and birthday.
Competitor Comparisons
:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Pros of Bogus
- More mature project with a longer history and larger community
- Extensive documentation and examples available
- Supports localization for multiple languages out of the box
Cons of Bogus
- Limited to .NET ecosystem, while Datafaker supports multiple languages
- Less frequent updates and releases compared to Datafaker
- Smaller set of built-in data providers
Code Comparison
Bogus:
var faker = new Faker("en");
var user = new Faker<User>()
.RuleFor(u => u.FirstName, f => f.Name.FirstName())
.RuleFor(u => u.LastName, f => f.Name.LastName())
.Generate();
Datafaker:
Faker faker = new Faker();
User user = new User(
faker.name().firstName(),
faker.name().lastName()
);
Both libraries offer similar functionality for generating fake data, with slight differences in syntax and method names. Bogus uses a fluent interface with rules, while Datafaker uses direct method calls on the faker instance.
Brings the popular ruby faker gem to Java
Pros of java-faker
- More established project with longer history and larger community
- Extensive documentation and examples available
- Supports a wide range of locales for internationalization
Cons of java-faker
- Less frequent updates and slower development cycle
- Limited support for custom data providers
- Lacks some advanced features found in newer libraries
Code Comparison
java-faker:
Faker faker = new Faker();
String name = faker.name().fullName();
String streetAddress = faker.address().streetAddress();
datafaker:
Faker faker = new Faker();
String name = faker.name().fullName();
String streetAddress = faker.address().streetAddress();
The basic usage of both libraries is very similar, with nearly identical method calls for generating fake data. However, datafaker offers additional features and customization options not shown in this basic example.
datafaker builds upon the foundation laid by java-faker, extending its functionality while maintaining compatibility with existing code. It provides more frequent updates, additional data providers, and advanced features like customizable locales and improved performance.
While java-faker remains a solid choice for basic fake data generation, datafaker offers a more modern and feature-rich alternative, especially for projects requiring extensive customization or advanced data generation capabilities.
A library for generating fake data such as names, addresses, and phone numbers.
Pros of Faker
- Mature and well-established library with a large community
- Extensive documentation and examples
- Supports multiple locales for internationalization
Cons of Faker
- Limited to Ruby programming language
- Slower performance compared to Datafaker
- Less frequent updates and maintenance
Code Comparison
Faker (Ruby):
require 'faker'
Faker::Name.name
Faker::Internet.email
Faker::Address.street_address
Datafaker (.NET):
using Bogus;
var faker = new Faker();
faker.Name.FullName();
faker.Internet.Email();
faker.Address.StreetAddress();
Key Differences
- Language: Faker is for Ruby, while Datafaker is for .NET
- Performance: Datafaker generally offers better performance
- API: Similar API structure, but with language-specific implementations
- Community: Faker has a larger community due to its longer existence
- Features: Both offer similar core features, but Datafaker includes some additional functionalities
Use Cases
- Faker: Ideal for Ruby projects requiring fake data generation
- Datafaker: Best suited for .NET applications needing efficient data faking
Both libraries serve similar purposes but cater to different programming ecosystems. The choice between them largely depends on the development platform and specific project requirements.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Datafaker
This library is a modern fork of java-faker with up to date libraries and several newly added Fake Generators.
Datafaker 2.x has Java 17 as the minimum requirement.
If Java 17 is not an option for you, you can choose to use Datafaker 1.x. Datafaker 1.x is built on Java 8, but this version is no longer maintained. We recommend all users to upgrade to Datafaker 2.x.
This library generates fake data, similar to other fake data generators, such as:
- Ruby's faker gem
- Perl's Data::Faker library
- Python faker package
- PHP faker library
- Javascript Faker.js library
It's useful when you're developing a new project and need some pretty data for showcase.
Usage
In the pom.xml, add the following fragment to the dependencies
section:
<dependency>
<groupId>net.datafaker</groupId>
<artifactId>datafaker</artifactId>
<version>2.3.1</version>
</dependency>
For Gradle users, add the following to your build.gradle file.
dependencies {
implementation 'net.datafaker:datafaker:2.3.1'
}
You can also use the snapshot version (2.3.2-SNAPSHOT
), which automatically gets published
after every push to the main branch of this repository. Binary repository URL for snapshots download is
https://s01.oss.sonatype.org/content/repositories/snapshots/
.
Get started
In your Java code:
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
Or in your Kotlin code:
val faker = Faker()
val name = faker.name().fullName() // Miss Samanta Schmidt
val firstName = faker.name().firstName() // Emory
val lastName = faker.name().lastName() // Barton
val streetAddress = faker.address().streetAddress() // 60018 Sawayn Brooks Suite 449
JShell
# from project root folder
jshell --class-path $(ls -d target/*.jar | tr '\n' ':')
| Welcome to JShell -- Version 17.0.4
| For an introduction type: /help intro
jshell> import net.datafaker.Faker;
jshell> var faker = new Faker();
faker ==> net.datafaker.Faker@c4437c4
jshell> faker.address().city();
$3 ==> "Brittneymouth"
jshell> faker.name().fullName();
$5 ==> "Vernie Schmidt"
Expressions
Faker faker = new Faker();
faker.expression("#{letterify 'test????test'}"); // testqwastest
faker.expression("#{numerify '#test#'}"); // 3test5
faker.expression("#{templatify 'test','t','q','@'}"); // @esq
faker.expression("#{examplify 'test'}"); // ghjk
faker.expression("#{regexify '[a-z]{4,10}'}"); // wbevoa
faker.expression("#{options.option '23','2','5','$','%','*'}"); // *
faker.expression("#{date.birthday 'yy DDD hh:mm:ss'}"); // 61 327 08:11:45
faker.expression("#{csv '1','name_column','#{Name.first_name}','last_name_column','#{Name.last_name}'}");
// "name_column","last_name_column"
// "Sabrina","Kihn"
faker.expression("#{json 'person','#{json ''first_name'',''#{Name.first_name}'',''last_name'',''#{Name.last_name}''}','address','#{json ''country'',''#{Address.country}'',''city'',''#{Address.city}''}'}");
// {"person": {"first_name": "Barbie", "last_name": "Durgan"}, "address": {"country": "Albania", "city": "East Catarinahaven"}}
also more examples at https://www.datafaker.net/documentation/expressions/
Collections
Faker faker = new Faker();
List<String> names = faker.collection(
() -> faker.name().firstName(),
() -> faker.name().lastName())
.len(3, 5)
.generate();
System.out.println(names);
// [Skiles, O'Connell, Lorenzo, West]
more examples about that at https://www.datafaker.net/documentation/sequences/
Streams
Faker faker = new Faker();
// generate an infinite stream
Stream<String> names = faker.stream(
() -> faker.name().firstName(),
() -> faker.name().lastName())
.generate();
Formats
Schema
There are 2 ways of data generation in specific formats
- Generate it from scratch
- There is already a sequence of objects and we could extract from them some values and return it in specific format
For both cases we need a Schema
which could describe fields and a way of data generation.
In case of generation from scratch Suppliers
are enough, in case of transformation Functions
are required
CSV
// transformer could be the same for both
CsvTransformer<Name> transformer =
CsvTransformer.<Name>builder().header(true).separator(",").build();
// Schema for from scratch
Schema<Name, String> fromScratch =
Schema.of(field("firstName", () -> faker.name().firstName()),
field("lastname", () -> faker.name().lastName()));
System.out.println(transformer.generate(fromScratch, 2));
// POSSIBLE OUTPUT
// "first_name" ; "last_name"
// "Kimberely" ; "Considine"
// "Mariela" ; "Krajcik"
// ----------------------
// Schema for transformations
Schema<Name, String> schemaForTransformations =
Schema.of(field("firstName", Name::firstName),
field("lastname", Name::lastName));
// Here we pass a collection of Name objects and extract first and lastnames from each element
System.out.println(
transformer.generate(
faker.collection(faker::name).maxLen(2).generate(), schemaForTransformations));
// POSSIBLE OUTPUT
// "first_name" ; "last_name"
// "Kimberely" ; "Considine"
// "Mariela" ; "Krajcik"
JShell
# from project root folder
jshell --class-path $(ls -d target/*.jar | tr '\n' ':')
| Welcome to JShell -- Version 17.0.4
| For an introduction type: /help intro
jshell> import net.datafaker.Faker;
jshell> import net.datafaker.providers.base.Name;
jshell> import net.datafaker.transformations.Schema;
jshell> import net.datafaker.transformations.CsvTransformer;
jshell> import static net.datafaker.transformations.Field.field;
jshell> var faker = new Faker();
faker ==> net.datafaker.Faker@c4437c4
jshell> Schema fromScratch =
...> Schema.of(field("firstName", () -> faker.name().firstName()),
...> field("lastname", () -> faker.name().lastName()));
fromScratch ==> net.datafaker.transformations.Schema@306a30c7
jshell> CsvTransformer<Name> transformer =
...> CsvTransformer.<Name>builder().header(false).separator(",").build();
transformer ==> net.datafaker.transformations.CsvTransformer@506c589e
jshell> System.out.println(transformer.generate(fromScratch, 2));
"firstName","lastname"
"Darcel","Schuppe"
"Noelle","Smitham"
JSON
Schema<Object, ?> schema = Schema.of(
field("firstName", () -> faker.name().firstName()),
field("lastName", () -> faker.name().lastName())
);
JsonTransformer<Object> transformer = JsonTransformer.builder().build();
String json = transformer.generate(schema, 2);
// [{"firstName": "Oleta", "lastName": "Toy"},
// {"firstName": "Gerard", "lastName": "Windler"}]
More complex examples and other formats like YAML, XML could be found at https://www.datafaker.net/documentation/formats/
Unique Values
Faker faker = new Faker();
// The values returned in the following lines will never be the same.
String firstUniqueInstrument = faker.unique().fetchFromYaml("music.instruments"); // "Flute"
String secondUniqueInstrument = faker.unique().fetchFromYaml("music.instruments"); // "Clarinet"
More examples can be found in https://www.datafaker.net/documentation/unique-values
Custom provider
Add your own custom provider in your app following steps from https://www.datafaker.net/documentation/custom-providers/
Documentation
Contributions
See CONTRIBUTING.md
If this is your first time contributing then you may find it helpful to read FIRST_TIME_CONTRIBUTOR.md
Providers
The list below is not complete and shows only a part of available providers. To view the full list of providers, please follow the link: Full list of providers.
- Address
- Ancient
- Animal
- App
- Appliance
- Aqua Teen Hunger Force
- Artist
- Australia
- Avatar
- Aviation
- AWS
- Azure
- Babylon 5
- Back To The Future
- Barcode
- Baseball
- Basketball
- Battlefield 1
- Beer
- Big Bang Theory
- Blood Type
- Boardgame
- Bojack Horseman
- Book
- Bool
- Bossa Nova
- Brand
- Breaking Bad
- Brooklyn Nine-Nine
- Buffy
- Business
- CNPJ (Brazilian National Registry of Legal Entities)
- CPF (Brazilian individual taxpayer registry identification)
- Camera
- Cat
- Chuck Norris
- Clash of Clans
- Code
- Coin
- Color
- Commerce
- Community
- Company
- Compass
- Computer
- Control
- Country
- Credit Card Type
- Cricket
- Crypto
- Currency
- Date and Time
- DC Comics
- Demographic
- Departed
- Dessert
- Device
- Disease
- Doctor Who
- Dog
- Domain
- Doraemon
- Dota 2
- Dragon Ball
- Driving License
- Dumb and Dumber
- Dune
- Durations
- Educator
- Elden Ring
- Elder Scrolls
- Electrical Components
- Emoji
- England Football
- Esports
- Fallout
- Family Guy
- Famous Last Words
- File
- Final Space
- Finance
- Food
- Formula 1 (:racing_car:)
- Friends
- Fullmetal Alchemist: Brotherhood
- Funny Name
- Futurama
- Game Of Thrones
- Garment Size
- Gender
- Ghostbusters
- Grateful Dead
- Greek Philosopher
- Hacker
- Harry Potter
- Hashing
- Hearthstone
- Heroes of the Storm
- Hey Arnold
- Hipster
- Hitchhiker's Guide To The Galaxy
- Hobbit
- Hobby
- Horse
- House
- How I Met Your Mother
- IdNumber
- Industry Segments
- Internet
- Job
- Joke
- K-pop (Korean popular music)
- Kaamelott
- Language Code
- League Of Legends
- Lebowski
- Locality
- Lord Of The Rings
- Lorem
- Marketing
- Marvel Snap
- Mass Effect
- Matz
- MBTI
- Measurement
- Medical
- Military
- Minecraft
- Money
- Money Heist
- Mood
- Mountaineering
- Mountains
- Movie
- Music
- Name
- Naruto
- Nation
- Nato Phonetic Alphabet
- Nigeria
- Number
- One Piece
- Options
- Oscar Movie
- Overwatch
- Passport
- Password
- Phone Number
- Photography
- Planet
- Pokemon
- Princess Bride
- Programming Language
- Red Dead Redemption 2
- Relationship Terms
- Resident Evil
- Restaurant
- Rick and Morty
- Robin
- Rock Band
- RuPaul's Drag Race
- Science
- Seinfeld
- Shakespeare
- Silicon Valley
- Simpsons
- Sip
- Size
- Slack Emoji
- Soul Knight
- Space
- StarCraft
- StarTrek
- Stock
- Studio Ghibli
- Subscription
- Super Mario
- Superhero
- Tea
- Team
- The IT Crowd
- Time
- Touhou
- Tron
- Twin Peaks
- University
- Vehicle
- Verb
- Volleyball
- Weather
- Witcher
- Yoda
- Zelda
Usage with Locales
Faker faker = new Faker(new Locale("lang", "COUNTRY"));
For example:
String californiaZipCode = new Faker(new Locale("en", "US")).address().zipCodeByState("CA");
String albanianIdNumber = new Faker(new Locale("sq", "AL")).idNumber().valid();
String moldovanPhone = new Faker(new Locale("ru", "MD")).phoneNumber().cellPhone();
Note that most of the data depends on language, but some data depends purely on country (personal ID and phone numbers). In the example above,
- "en", "sq", "ru" are language codes (English, Albanian and Russian), and
- "US", "AL", "MD" are country codes (USA, Albanian and Moldova)
Supported Locales
- ar (Armenian)
- be_BY (Belarusian)
- bg_BG (Bulgarian)
- ca_CA
- ca-CAT
- cs_CZ (Czech republic)
- da-DK
- de
- de-AT
- de_CH (country: Switzerland, language: German)
- el-GR
- en
- en-AU
- en-au-ocker
- en-BORK
- en-CA
- en-GB
- en-IND
- en-MS
- en-NEP
- en-NG
- en-NZ
- en-PAK
- en-SG
- en-UG
- en-US
- en-ZA
- en-PH
- es_ES (Spain)
- es-MX (Mexican)
- et_EE (Estonian)
- fa
- fi-FI
- fr_FR (French)
- fr_CH (country: Switzerland, language: French)
- ge_GE (Georgia)
- he_IL (language: Hebrew, country: Israel)
- hr_HR (Croatian)
- hu_HU (Hungarian)
- hy_AM (Armenian)
- in-ID (Indonesia)
- it
- ja
- ka
- ko
- mk_MK (North Macedonia)
- nb-NO
- nl
- pl
- pt
- pt-BR
- ro_MD (country: Moldova, language: Moldavian a.k.a. Romanian)
- ru_RU (Russian)
- sk_SK (Slovak)
- sq_AL (Albanian)
- sv
- sv-SE
- tr
- th_TH (Thailand)
- uk
- vi
- zh_CN (Chinese)
- zh-TW
LICENSE
Copyright (c) 2024 Datafaker.net See the LICENSE file for license rights and limitations.
Top Related Projects
:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Brings the popular ruby faker gem to Java
A library for generating fake data such as names, addresses, and phone numbers.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot