Generate dummy data for various tests with Faker (java)

Introduction

Faker is a library that can be used to create test data such as name and address. There are libraries in various languages, and the following "Java Faker" seems to be famous in Java.

Java Faker https://github.com/DiUS/java-faker

First add the library

For Maven, add the library dependency as follows.

pom.xml


<dependency>
    <groupId>com.github.javafaker</groupId>
    <artifactId>javafaker</artifactId>
    <version>0.15</version>
</dependency>

Try to generate dummy data

It's easy to use, and if you write the following code,

	private void dummy() {
		Faker faker = new Faker(new Locale("ja_JP"));

		System.out.println(faker.name().fullName());
		System.out.println(faker.name().firstName());
		System.out.println(faker.name().lastName());
		System.out.println(faker.address().zipCode());
		System.out.println(String.join(faker.address().state(), faker.address().city(), faker.address().cityName()));
	}

Dummy data is generated as follows.

Makoto Takeuchi
Aimi
Goto
213-0541
Yoko-gun, Haramachi, Saitama Prefecture

You can also get the address with faker.address (). fullAddress (), It is not good because the order is output in reverse like "345 Future Terrace, Yamato City, 32 894-1403".

In addition, various data such as email addresses, file names, URLs, colors, books, etc. can be generated.

Try to specify the format

You can also generate dummy data by specifying the format as shown below. We have generated 10 email addresses for gmail.com.

	private void dummy2() {
		FakeValuesService fakeValuesService = new FakeValuesService(
			      new Locale("ja_JP"), new RandomService());
		IntStream.range(0, 10).forEach(i -> System.out.println(fakeValuesService.bothify("??####@gmail.com"))); // ?Is an alphabetic character,#Is a number
	}

Execution result

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

Generate a random number

To generate a 10-digit number, write as follows.

faker.number().randomNumber(10, true)

Below, the code and execution result

	private void dummy3() {
		Faker faker = new Faker(new Locale("ja_JP"));

		IntStream.range(0, 10).forEach(i -> System.out.println(faker.number().randomNumber(10, true)));
	}
1324687562263
3958356750281
8654092665509
4641056803394
1610358318650
1541823905182
1182616958335
7453193108824
7835740448808
9378928785264

Generate sentences

Generate sentences with 5 words. I changed Locale to jp, but Japanese was not generated.

faker.lorem().sentence(5)

	private void dummy4() {
		Faker faker = new Faker(new Locale("ja_JP"));
		IntStream.range(0, 10).forEach(i -> System.out.println(faker.lorem().sentence(5)));
	}
Sunt saepe fugiat sunt eum perferendis quia placeat voluptas similique.
Explicabo repellendus qui praesentium fugiat harum excepturi qui.
Architecto ea aliquid debitis in.
Omnis labore aut ut laudantium minima labore ut.
Facilis velit eum repudiandae sint commodi.
Consequatur voluptatem commodi incidunt consequuntur.
Iure exercitationem nihil optio laudantium provident aut.
Cumque earum hic eligendi cumque id quia quasi laborum rem.
Ut provident et corrupti sed dignissimos.
Facilis odit illo id et aliquam.

Generate sentences by specifying the number of characters

Generate a 500-character sentence.

faker.lorem().fixedString(500)

	private void dummy5() {
		Faker faker = new Faker(new Locale("ja_JP"));
		System.out.println(faker.lorem().fixedString(500));
	}
Sint soluta dolorum eos est vero quam.Earum consectetur qui corrupti.Similique fugit rerum velit et repellat corrupti facere.Et inventore commodi fugit.Aperiam dolorem laudantium.Quis ut consequatur qui facilis aliquam.Hic a vitae omnis quia sint id.Atque est in voluptatum quidem ut.Dolore dolor ex dolor fugiat.Error est et quae quia sit dolores voluptatem.Nihil tempora nobis illo.Et dolorem voluptatem laboriosam consequuntur.Repellat at recusandae amet voluptas quam consequatur est.Fuga eveniet

Generate with a regular expression.

Generate with a regular expression.

fakeValuesService.regexify("[12]\d{3}/(0?[1-9]|1[0-2])/([12][0-9]|3[01]|0?[1-9])")

	private void dummy6() {
	    FakeValuesService fakeValuesService = new FakeValuesService(
	    	      new Locale("ja_JP"), new RandomService());
	    System.out.println(fakeValuesService.regexify("[12]\\d{3}/(0?[1-9]|1[0-2])/([12][0-9]|3[01]|0?[1-9])"));
	}
1147/5/30

Recommended Posts

Generate dummy data for various tests with Faker (java)
Create realistic dummy data with gem Faker
Create dummy data of portfolio with Faker [Note]
Various method tests with MockRestServiceServer
Java for beginners, data hiding
Java Learning 1 (learning various data types)
Java unit tests with Mockito
[Rails] Prepare dummy data to be used for testing [Faker]
Enable OpenCV with java8. (For myself)
Getting Started with Ruby for Java Engineers
Learn Java with "So What" [For beginners]
Interoperability tips with Kotlin for Java developers
Write tests for JavaFX applications with TestFX
[Java] How to test for null with JUnit
Links & memos for getting started with Java (for myself)
Ask for n business days later with JAVA
Create related data together with FactoryBot for yourself
Docker Container Operations with Docker-Client API for Java
Generate Java client code for Salesforce SOAP API