Nerd For Tech
Published in

Nerd For Tech

Java Faker With IntelliJ Maven Project [Software Development Engineer in Test Article Series Part 8]

Up to now, I have written 7 articles which are mostly about the structure of software testing. From this article, I am starting the technical part of software testing. You can read the previous articles from here.

I assume that the reader knows coding with java, and is familiar with IntelliJ IDE.

Technical prerequisites:

  • IntelliJ IDE must be installed.
  • JDK must be installed.

Question: What is Java Faker?

Java Faker is a fake data generator.

Question: Why do we need it?

In order to perform software tests, we almost always need data to use in our test steps.

For example, you are testing a “sign up with email” functionality.

You need email addresses to write in the email text box. You need valid and invalid email addresses for testing both negative and positive test scenarios.

Thanks to Java Faker, you can always populate a random valid email address to use in your tests.

Question: Do developers use it?

Sometimes. They use Java Faker for unit tests. Like a SDET, they also need to test valid and email addresses in their unit tests.

Moreover, if the database or API is not ready yet, developers can use random data from Java Faker when they need sample data.

Where is Java Faker? Where to find it?

Java Faker is a public Java tool. It has a public repository on GitHub. And there is the document page of Java Faker.

How to use Java Faker?

First, you have to add Java Faker as a dependency to your project.

What project?

Any project coded with Java.

How to add?

Follow the pictures for adding Java Faker to an IntelliJ Maven project:

Open IntelliJ>Click New Project
Select Maven>Click Next
Name your project
Default Project View and pom.xml file content
To add Java Faker go to mvnrepository.com in your internet browser
Search Java Faker and click the first result
Click on the highest GREEN version
Click Maven tab and click inside the box, it will copy the content
create “dependencies” tag pair as shown
just paste the copied dependency content
either click 1 or open the Maven side window and click 2 to fetch dependency from the repository
create a new class under src>main>java
name your class
add a main method to your class

Now, we added Java Faker library to our project.

As you see in the last picture we created a Faker object.

As you noticed from the top of the class, that Faker class is defined in the “com.github.javafaker” package that we just added as a dependency.

Simple Faker Usage

In the image above; a Faker object is created and an animal name is generated. As you see, codes are the same but outputs are different. So Faker is populating randomly.

Another way of Faker object creation
Java Faker Localization

In the image above, there are two different Faker objects; one is created with Turkish Local, another is created with US Local. As you see the first name generation is different based on the local settings of instances.

What kind of topics do we have in Java Faker, like animals and names?

According to the documentation, these are the main data topics:

  • Address
  • Ancient
  • Animal
  • App
  • Aqua Teen Hunger Force
  • Artist
  • Avatar
  • Back To The Future
  • Aviation
  • Basketball
  • Beer
  • Bojack Horseman
  • Book
  • Bool
  • Business
  • ChuckNorris
  • Cat
  • Code
  • Coin
  • Color
  • Commerce
  • Company
  • Crypto
  • DateAndTime
  • Demographic
  • Disease
  • Dog
  • DragonBall
  • Dune
  • Educator
  • Esports
  • EnglandFootBall
  • File
  • Finance
  • Food
  • Friends
  • FunnyName
  • GameOfThrones
  • Gender
  • Hacker
  • HarryPotter
  • Hipster
  • HitchhikersGuideToTheGalaxy
  • Hobbit
  • HowIMetYourMother
  • IdNumber
  • Internet
  • Job
  • Kaamelott
  • LeagueOfLegends
  • Lebowski
  • LordOfTheRings
  • Lorem
  • Matz
  • Music
  • Name
  • Nation
  • Number
  • Options
  • Overwatch
  • PhoneNumber
  • Photography
  • Pokemon
  • Princess Bride
  • Relationship Terms
  • RickAndMorty
  • Robin
  • RockBand
  • Shakespeare
  • Sip
  • SlackEmoji
  • Space
  • StarCraft
  • StarTrek
  • Stock
  • Superhero
  • Team
  • TwinPeaks
  • University
  • Weather
  • Witcher
  • Yoda
  • Zelda

What kind of data do those main topics generate?

They all generate String data.

There are methods in those classes. These methods generate data mostly without parameters.

For example in the Address class there is a method

streetAddress()

and there is an overloaded version with a boolean parameter

streetAddress(boolean includeSecondary)

Let’s look the outputs:

streetAddress function overloading

What are all of these methods in all of those main classes?

I had the same curiosity. Then, I wrote a couple of codes to get all of the available methods with Java reflection. I iterate the main classes and their methods that have no parameters. I run those methods with a default Faker object and with a Turkish Localized Faker object. So, at the end, I could get a list of sample outputs based on two different localizations. The default local is EN. There is the GitHub of that short project: JavaFakerSamples. Just copy the class to your project or clone the all repository.

I put the outputs that I yielded from the JavaFakerSamples project in a Google Spreadsheet: JavaFaker Methods Without Parameters Outputs EN/TR.

Screenshot of the Faker Outputs document

With this output, you can have an opinion about the Faker methods and you become familiar with the localized outputs.

What else? Are we done with Java Faker?

There are some useful functions which can be used through the Faker object.

The numerify method returns a string with the ‘#’ characters, in the parameter, replaced with random numbers.

numerify method samples

The letterify method returns a string with the ‘?’ characters, in the parameter, replaced with random characters.

letterify method samples

The bothify method returns a string with the ‘?’ characters, in the parameter, replaced with random characters, and ‘#’ chracters replaced with random numbers.

bothify method samples

The regexify method returns a random sequence based on a input regex pattern.

regexify method examples

Lastly, I want to share the Random Service outputs:

Random Service Samples

Ok. We generated a lot of different data. I have power of random data generation. Where to use them?

In following articles we will use that data. This is an only preparation for our tests.

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

VNode Setup Guide

Flutter Circle Avatar and ListView.builder()

Agility CMS, Next.js & Snipcart Tutorial

Amazon EC2 Setup with Ubuntu and XAMPP Installation

Five Minute Kubernetes Installation With k3OS

My First Twitter Bot Using Python and Heroku

Stovepipe Enterprise — Your Daily Software Anti-Pattern

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hüseyin K.

Hüseyin K.

Software Developer & SDET/QA

More from Medium

what is Garbage Collector, Stack ,Heap in JAVA?

Java Packages & API-10 example

Java Design Patterns: Singleton

Singleton Design pattern in Java