top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Generating Fake Data to create Payload in Rest API

The agile transformation of software testing has been accelerating since the pandemic and there is no slowing down. Applications need to be tested faster and earlier in the software development lifecycle, while customer experience is a rising priority.  This is where synthetic data generation comes in. This technique involves creating data that appears and behaves like real data but is not based on actual data points. There are many benefits to using synthetic data for testing, including improved privacy, cost savings, and higher efficiency.

In this blog post, we will focus on the topic of synthetic data generation for UI and API Testing using Java library called “Java Faker” and its significance in automation testing. We will also go over two popular techniques for generating synthetic data: generating fake data and generating dummy data. Eventually, we will argue that synthetic data generation is a valuable tool that can greatly improve the effectiveness and accuracy of testing, while also ensuring data privacy and security.


What is test data?

·       The test data required to test the application is called test Data.

·       The test data depends on the type of test we need to perform . Application testing is made up of lots of different parts, many of which require test data.

·       Unit tests are the first to take place in the software development process. Test data for unit tests consist of simple, typically small samples of test data.

·       Performance testing or load testing requires large batches of test data. However, in any of above cases, Production data is not test data. Production data should never be in test environments.

·       Data masking, randomization, and other common techniques do not anonymize data adequately.

·       Mock data and AI-generated synthetic data are privacy-safe options. The type of test should decide which test data generation should be used.

·       There are wide varieties of test data such as Person details, Employee details , Customer Details etc.

·       Depending on the location of TestData we have different locales available in the java library.


Types of Fake Data Generation Techniques:

There are several techniques that can be used to generate fake data. These include:

Rule-based: This method involves creating rules or algorithms that imitate real-world data patterns. A rule-based generator, for example, may generate data that follows a specific statistical distribution, such as a normal distribution.

Machine learning-based: This means using real-world data to train machine learning models to discover patterns and generate new data that is similar to the original data.

Randomization: This involves generating data values at random without any specific rules or patterns

 

Below picture shows different software testing that involves test data to perform


Randomly generated Fake Datasets:

Lets explore how can we use the Java Faker library to generate random and effective data that can be useful for us in our test automation workflows and scripts.  It is generated in the same format and with the same properties as real data, but it contains no actual values or sensitive information.

The Library which can be used for this purpose isJava Faker


By following the below steps , we can achieve the Fake data generation


Step 1-

In Rest-Assured API Framework, Please make sure to add the below faker dependency in pom.xml

<dependency>

    <groupId>net.datafaker</groupId>

    <artifactId>datafaker</artifactId>

    <version>2.2.2</version>

</dependency> 

Step 2-

Create a Package “JavaFaker.Tests” under the project Phase2_LMS_API_RestAssured

Now create an object for Faker Class and using the faker class , we could able to generate fake data for batchName, batchdescription , batchNumberofClasses etc.

      public static Map<String, Object> getorcreateBatchBata(String BatchName, String BatchDesc, String BatchStatus , String BatchNoofClasses)

      {

            Faker fakeBatch = new Faker();

            Map<String, Object> payload = new HashMap<>();

            payload.put("BatchName", fakeBatch.name().fullName());

            payload.put("BatchDesc", fakeBatch.name().title());

       payload.put("BatchNoofClasses",fakeBatch.number().randomDigitNotZero());

           

            return payload;        

      }

Instead of Passing the hardcoded values as shown below to the payload , using faker class to generate fake data will avoid changing the payload values manually each time we execute the CRUD Operation.

Every time we test the CRUD operation , new payload will be automatically created by using Faker library.

     

public static Map<String, Object> getorcreateBatchBata(String BatchName, String BatchDesc, String BatchStatus , String BatchNoofClasses)

      {

            Map<String, Object> payload = new HashMap<>();

            payload.put("BatchName", "TechJava");

            payload.put("BatchDesc", "TechJavaTitile");

            payload.put("BatchNoofClasses","4");

            payload.put("BatchStatus","Active");

           

            return payload;        

      }

Java Faker Library:

This Java library generates dummy data which looks similar to the Real Data . It Supports 35 domains such as

Finance

Banking

Food

Address

Library

Business and so on.

The Library also supports different locales like en-SG

  • en-UG

  • en-US

  • en-ZA

Faker faker = new Faker(new Locale("YOUR_LOCALE"));

Reference to the more details of faker class can be found https://github.com/DiUS/java-faker

  

One important point which you need to note is that to get started with Faker you will need to create an object for the Faker class. Using this faker object we will access other available methods that can be used to generate fake data.

There is also an option to pass in specific locales (Locale.UK/Locale.US)

Faker faker = new Faker();

Faker fakerWithLocales = new Faker(Locale.UK)


Address:

This can be used to generate random address data including city, country, full address, etc, let’s explore a few cases.

The below methods can be used to get City, Country, and State details The way in which we can use the faker methods is mentioned below, for now, I am just storing it in some variables.

import com.github.javafaker.Faker;

Faker faker = new Faker();

String city = faker.address().city();String state = faker.address().state();String country = faker.address().country();

Similarly, we can use the below methods to retrieve street and zip code

String streetName = faker.address().streetName();String streetAddrss = faker.address().streetAddress();String zipCode = faker.address().zipCode();


Finance:

Below mentioned are the methods that can be used to get credit card number details based on the specific type: VISA, MASTERCARD, AMERICAN EXPRESS

faker.finance().creditCard(CreditCardType.AMERICAN_EXPRESS);faker.finance().creditCard(CreditCardType.VISA);faker.finance().creditCard(CreditCardType.MASTERCARD);

Internet

To get fake email address details use the below method

faker.internet().emailAddress();

To get fake password details, we have a variety of overloaded methods that can be used accordingly as per your needs. These are self-explanatory.


Phone Number:

To get a cellphone or phone number details you can use the below methods

faker.phoneNumber().cellPhone();faker.phoneNumber().phoneNumber();


Name:

Under the Name class, we have a wide number of methods available that can be used.

faker.name().prefix();faker.name().firstName();faker.name().lastName();faker.name().suffix();faker.name().fullName();faker.name().nameWithMiddle();

Additionally, we have a username() method which will return A lowercase username composed of the first_name and last_name joined with a ‘.’

For Example: tim.smith or jim.davis

faker.name().username();


Number:

To get some random number or digit you can use the below methods, you can pass in the appropriate set of parameters to get a digit or random number based on your application use cases.


DateAndTime

There are few methods available that can be used to generate between dates, past and future date values based on relative days criteria.

Generate Between Date:

Parameters: from — the lower bound inclusiveto — the upper bound exclusive

faker.date().between(Date from, Date to);

Generate Future Date:

int atMost — at most this amount of time ahead from now exclusive.TimeUnit unit — the time unit. (Ex: TimeUnit.Days)

faker.date().future(int atMost,TimeUnit unit);

int atMost — at most this amount of time ahead from now exclusive.int minimum — the minimum amount of time in the future from now.TimeUnit unit — the time unit. 

faker.date().future(int atMost, int minimum, TimeUnit unit);

int atMost — at most this amount of time ahead to the referenceDate exclusive.TimeUnit unit — the time unit.Date referenceDate — the future date relative to this date.

faker.date().future(int atMost, TimeUnit unit, Date referenceDate);


Advantages of Fake Data Generation:

There are few advantages by using synthetic data such as

Data Privacy:

Due to privacy and security, Real world data cannot be used during testing . Hence , Fake data is generated with similar pattern that simulate real-data while avoiding the risk of messing the original data .

Testing and Quality Assurance:

Application Testing involves bulk data to cover all the positive and  negative scenarios to ensure the stability and functionality of the application. Hence , Fake Data helps in reduced time involved for testing as well improve the testing efficiency.

Cost Savings:

Working with Real-world Data involves more cost and maintenance whereas dummy Data involves less cost , low maintenance and storage .

Compliance: Dummy data can be used to ensure that data privacy regulations such as GDPR and HIPAA are followed.

Overall, dummy data generation is a good practice for developers and testers who want to protect sensitive data during testing and developing new applications. It allows them to create a dataset with a similar structure to real data while protecting the privacy and security of sensitive information.


17 views0 comments

コメント

5つ星のうち0と評価されています。
まだ評価がありません

評価を追加
bottom of page