Once our new prototype is ready for a first demo, we usually need some meaningful data to present it. Using foo, bar, duke, or other tech-related terms may not be the best option when showcasing software to a customer. This should be no problem if we already have a solid data source, but what about the opposite case? How to simply create meaningful random data for our Java application?
In the past, I've only used the UUID
and ThreadLocalRandom
Java classes to create random data. As these are really limited and don't offer domain-specific random data, I searched for a better solution. While searching, I found the Faker gem for Ruby and, luckily, a Java port of it: Java Faker. With this easy-to-use library, we get access to many domains (e.g., books, food, persons, city, etc.) and can generate meaningful random data in seconds.
Update: As there's little maintenance activity in the Java Faker library, this article was updated to use the successor Data Faker.
Getting Started with Data Faker (former Java Faker)
We can include Data Faker (formerly Java Faker) in our Maven project with the following dependency:
1 2 3 4 5 | <dependency> <groupId>net.datafaker</groupId> <artifactId>datafaker</artifactId> <version>1.4.0</version> </dependency> |
To start using random data, we create an instance of the Faker
class:
1 | Faker faker = new Faker(); |
We can create a Faker
instance once and don't have to instantiate an object whenever we need random data as the methods randomly access the underlying data. The library uses .yml
files in /src/main/resources
as its internal data store to retrieve random data.
With this library, we can access more than 30 different domains and create random data for nearly every use case. We can generate random data for a specific domain using the fluent API:
1 2 3 | String firstName = faker.name().firstName(); String appName = faker.app().name(); String foodIngredient = faker.food().ingredient(); |
These domains include:
- Finance
- Food
- Books
- Name
- Address
- Business
- …
and also funny domains like:
- Yoda
- ChuckNorris
- HarryPotter
- LordOfTheRings
- …
To get a list of all available Fakers (domains), have a look at the README of the project.
Spring Boot Example with Data Faker (former Java Faker)
To demonstrate an example of this library in action, we're developing an API endpoint to return random persons, books, and food. We use Spring Boot 2.7.0 and Java 17, but any other Java setup would also work.
The sample application offers different endpoints to get random data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | @RestController @RequestMapping("/random") public class RandomDataEndpoint { private final ObjectMapper objectMapper; public RandomDataEndpoint(ObjectMapper objectMapper) { this.objectMapper = objectMapper; } @GetMapping("/persons") public JsonNode getRandomPersons() { Faker faker = new Faker(); ArrayNode persons = objectMapper.createArrayNode(); for (int i = 0; i < 10; i++) { persons.add(objectMapper.createObjectNode() .put("firstName", faker.name().firstName()) .put("lastName", faker.name().lastName()) .put("title", faker.name().title()) .put("suffix", faker.name().suffix()) .put("address", faker.address().streetAddress()) .put("city", faker.address().cityName()) .put("country", faker.address().country())); } return persons; } } |
Once we run the application, we can get a list of random persons at http://localhost:8080/random/persons:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [ { "firstName": "Maegan", "lastName": "Murray", "title": "Dynamic Communications Planner", "suffix": "PhD", "address": "2976 Argentina Freeway", "city": "Altheatown", "country": "Dominica" }, { "firstName": "Ulysses", "lastName": "O'Keefe", "title": "Senior Operations Designer", "suffix": "Jr.", "address": "3319 Durgan Crescent", "city": "Shericetown", "country": "Paraguay" } ] |
Return Localized Random Data
The default locale for the random data is EN. In addition, the library offers the capability to return localized random data. If we specify a locale, we have to make sure it's supported by having a look at the corresponding .yml
file to check if someone translated the English examples.
If there is no translation for our locale, e.g., beers, the library will fall back to English.
The locale is specified as a constructor argument of the Faker
class:
1 | Faker faker = new Faker(new Locale("de")); |
This object instance is now capable of returning random data in German:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @GetMapping("/foods") public JsonNode getRandomFoods() { Faker faker = new Faker(new Locale("de")); ArrayNode foods = objectMapper.createArrayNode(); for (int i = 0; i < 10; i++) { foods.add(objectMapper.createObjectNode() .put("ingredients", faker.food().ingredient()) .put("spices", faker.food().spice()) .put("measurements", faker.food().measurement())); } return foods; } |
Or any other locale which is listed in the README of the project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @GetMapping("/books") public JsonNode getRandomBook() { Faker faker = new Faker(new Locale("en-US")); ArrayNode books = objectMapper.createArrayNode(); for (int i = 0; i < 10; i++) { books.add(objectMapper.createObjectNode() .put("author", faker.book().author()) .put("genre", faker.book().genre()) .put("publisher", faker.book().publisher()) .put("title", faker.book().title())); } return books; } |
The source code for this example is available on GitHub.
If you're working with Kotlin, have a look at kotlin-faker.
Have fun creating random data with Java Faker,
Philip