[EN] Java Faker - Custom Fakes

Julian | Jul 4, 2024 min read

As a beginner in the Java library “Faker”, there are many interesting ways you can generate custom fake data. I will explain to you step by step how you can create your own fakes and use Faker’s flexibility.

1. Faker Basics

First of all, a quick refresher: Faker is a library that helps you create different types of realistic-looking fake data, such as names, addresses, email addresses, etc. There are many built-in categories, but sometimes you have specific needs that are not directly supported.

2. The regexify method

regexify is a method in Faker that allows you to generate data based on regular expressions. This is very useful because it gives you a lot of control over the data generated.

Here is a simple example:

import com.github.javafaker.Faker; 

public class Main { 
public static void main(String[] args) { 
Faker faker = new Faker(); 

// Generates a random alphanumeric string with a length of 10 
String customField = faker.regexify("[a-z1-9]{10}"); 
System.out.println(customField); // e.g. "a3b5d6e7f8" 

// Generates a random phone number in the format (###) ###-#### 
String phoneNumber = faker.regexify("\\(\\d{3}\\) \\d{3}-\\d{4}"); 
System.out.println(phoneNumber); // e.g. "(123) 456-7890" 
} 
} 

3. Add custom methods

Sometimes you want to add custom methods to the Faker class. You can do this by creating your own class and integrating it with Faker. Here is a quick example.

Create your own Faker class

import com.github.javafaker.Faker; 

public class MyCustomFaker { 
private final Faker faker; 

public MyCustomFaker(Faker faker) { 
this.faker = faker; 
} 

public String myCustomData() { 
// A method that generates custom data 
return "CustomData-" + faker.number().digits(5); 
} 
} 

Integrate the custom class

Now you need to use the class in your main program:

import com.github.javafaker.Faker; 

public class Main { 
public static void main(String[] args) { 
Faker faker = new Faker(); 
MyCustomFaker myFaker = new MyCustomFaker(faker); 

System.out.println(myFaker.myCustomData()); // e.g. "CustomData-12345" 
} 
} 

4. Using @Factory (Advanced option)

In advanced scenarios, you can also generate custom data objects using the @Factory annotation. This is more advanced and usually not necessary for simple needs, but here’s an example in case you want to try it:

Create a custom data class

public class CustomData { 
private final String customField; 

public CustomData(String customField) { 
this.customField = customField; 
} 

public String getCustomField() { 
return customField; 
} 
} 

Create a factory class

import com.github.javafaker.Faker; 

public class CustomDataFactory { 
private final Faker faker; 

public CustomDataFactory(Faker faker) { 
this.faker = faker; 
} 

public CustomData create() { 
String customField = faker.regexify("[A-Z]{10}"); 
return new CustomData(customField); 
} 
} 

Use the factory class in your main program

import com.github.javafaker.Faker; 

public class Main { 
public static void main(String[] args) { 
Faker faker = new Faker(); 
CustomDataFactory factory = new CustomDataFactory(faker); 

CustomData customData = factory.create(); 
System.out.println(customData.getCustomField()); // e.g. "QWERYTUIOP" 
} 
} 

5. Best Practices

  • Use regular expressions: If you need high flexibility, regular expressions help you create specific data patterns.
  • Create Reusable Components: Create separate classes and methods for custom data that you can reuse.
  • Ensure data is realistic: Make sure the data generated looks realistic for better testing.

With these steps and examples, you should be able to leverage Faker’s flexibility to generate custom fake data that meets your exact needs. Good luck implementing it!