It is a library that can be used to generate a wide array of real-looking data from a mobile number, addresses, names to popular culture references. This is really helpful when we want to use some placeholder but don't have actual data. Like you want to generate credit card data and want to do validation against that. Faker library will help you to generate that.
In the microservices-based development, we need data to do against validation, and generate the dummy test data is quite a challenging task. In this article, we will be looking at how to resolve this using Faker
add below dependency,
Maven:
<dependency><groupId>com.github.javafaker</groupId><artifactId>javafaker</artifactId><version>1.0.2</version></dependency>
Gradle
compile group: 'com.github.javafaker', name: 'javafaker', version: '1.0.2'
Just make fun and start using random data, just create below
Faker feku = new Faker();
If you want to generate with different locales:
Faker faker = new Faker(new Locale("YOUR_LOCALE"));
//e.g en-US
Faker feku = new Faker(new Locale("en-US"));
This library provides you access to more than 35 different domains and provides data nearly to every real-life use case. The domains like below
- Finance
- Food
- Books
- Name
- Address
- Business
- ChuckNorris
- HarryPotter
- LordOfTheRings
Check all the list of available domain and locale it supports:
https://github.com/DiUS/java-faker
Example:
package utilities;
import java.nio.charset.Charset;
import java.util.Random;
import com.github.javafaker.Faker;
public class GenerateFakeTestData {
private String email;
private String firstname;
private String lastname;
private String password;
private String companyName;
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
Faker fkobj=new Faker();
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static String getAlphaNumericString(int n) {
// length is bounded by 256 Character
byte[] array = new byte[256];
new Random().nextBytes(array);
String randomString = new String(array, Charset.forName("UTF-8"));
// Create a StringBuffer to store the result
StringBuffer r = new StringBuffer();
// Append first 20 alphanumeric characters
// from the generated random String into the result
for (int k = 0; k < randomString.length(); k++) {
char ch = randomString.charAt(k);
if (((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) && (n > 0)) {
r.append(ch);
n--;
}
}
// return the resultant string
return r.toString();
}
}
-----------------------------------------------
Usage of FAKER class
package utilities;
import java.util.Locale;
import com.github.javafaker.Faker;
import com.github.javafaker.service.FakeValuesService;
import com.github.javafaker.service.RandomService;
public class ConsumeTestData {
public static void main(String[] args) {
//Create Object for Faker class
Faker fkobj=new Faker();
GenerateFakeTestData tdobj=new GenerateFakeTestData();
String firstName=fkobj.address().firstName();
tdobj.setFirstname(firstName);
System.out.println("firstname is:"+tdobj.getFirstname());
//generate last name
String lastName=fkobj.address().lastName();
tdobj.setLastname(lastName);
System.out.println("lastname is:"+tdobj.getLastname());
String email=fkobj.internet().emailAddress();
tdobj.setEmail(email);
System.out.println("email address is:"+tdobj.getEmail());
String company=fkobj.company().name();
tdobj.setCompanyName(company);
System.out.println("company name is:"+tdobj.getCompanyName());
tdobj.setPassword(tdobj.getAlphaNumericString(10));
System.out.println("random password:"+tdobj.getPassword());
System.out.println("*****************************************");
System.out.println("Generate the testdata with indian locale");
// Create an object of Faker class with default locale i.e ENG
Faker fkobj1 = new Faker(new Locale("en-IND"));
// To generate valid random first name
System.out.println("First name is: "+fkobj1.name().firstName());
// To generate valid random last name
System.out.println("Last name is: "+fkobj1.name().lastName());
// To generate valid random city name
System.out.println("City name is: "+fkobj1.address().cityName());
// To generate valid random state name
System.out.println("State name is: "+fkobj1.address().state());
// To generate valid random country name
System.out.println("Country name is: "+fkobj1.address().country());
// To generate valid random cell number
System.out.println("Cell number is: "+fkobj1.phoneNumber().cellPhone());
// To generate valid random animal name
System.out.println("Animal name is: "+fkobj1.animal().name());
}
}