Builder Pattern to the rescue!
I bet you might have come across a scenario while Automating API/Web or Mobile Application where while registering a user or may be setting the address for checking out a product in the end to end test journey of the product you have to fill in the details on the user.
So, How do you do that?
Normally, We create a POJO class in Java with the fields required to register user or to set the address for checking out the product and then set the values in the test using the constructor of the POJO class.
Let’s take an example of registering a user where following are mandatory fields required to fill in the registration form:
- First Name
- Last Name
- Mobile Number
As we need to handle these fields in automation testing we will have to pass on respective values in the fields when the test is executed.
Before Using the Builder Pattern
We would be creating a POJO class with the above mentioned mandatory fields and generate Getters and Setters inside that class and using a constructor we set the values in the respective fields.
Checkout the code example of RegisterUser class below for the representation of what we are discussing:
Now, if we want to use this POJO we would have to create an instance of RegisterUser class and pass the values in the constructor parameters as given in the code example below to set the data in the respective fields.
Checkout the below example Register User test of how we do it:
There were just seven fields in the example we took for registering the user. However, this would not be the case with every website and application. There would be some more additional fields required and as the fields keep on increasing every time, we would need to update the POJO class with respective Getters and Setters and also update the parameters in the constructor.
Finally, we would need to add the values to those field so the data could be passed in the actual field required.
Long story short, we would need to refactor the code even if there is single new field added, also, it doesn’t look clean to add values as parameter in the tests.
Luckily, Builder Pattern comes to the rescue here.
All of the code which is showcased in this blog is available in this GitHub repository.
Do mark a ⭐ and make the project popular so it reaches to the people who want to learn and upgrade and learn something new related to test automation.
What is Builder Pattern?
Builder is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code. [Source]
Builder Pattern helps us solve the issue of setting the parameters by providing a way to build the objects step by step by providing a method that returns the final object which can be used in the actual tests.
What is Lombok?
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. It is an annotation based java library that helps in reducing the boilerplate code.
It helps us in writing short and crisp code without having to write the boilerplate code. By passing the
@Getterannotation, it automatically generates Getter methods. Similarly you don’t have to write the code for Setters, its
@Setterannotation automatically generates the Setter methods.
It also has the support for using the Builder Pattern so we just need to put the
@Builderannotation above the class and rest Lombok will take care of.
To use Lombok in the project we need to add the following Maven dependency:
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
Using the Builder Pattern with Lombok
Before we start refactoring the code we have written, let me tell you about the Java Faker library as well as it helps in generating fake data that can be used for testing. Ideally, every new registered user’s data should be unique otherwise we may get duplicate data error and test will fail.
Here, Java Faker library will help us in providing unique data in each test execution thereby helping us with registering a new user with unique data every time the registration test is run.
To use Java Faker library, we need to add the following Maven dependency in our project:
<!-- https://mvnrepository.com/artifact/com.github.javafaker/javafaker -->
Now, lets start refactoring the code. First, we will make the changes to the RegisterUser class. We would be removing all the Getters and Setters and also the Constructor and add the @Getter and @Builder annotation tags on the top of the RegisterUser class.
Here is how the RegisterUser class looks now after the refactoring :
How clean and crisp it looks with that refactoring being done. Multiple lines of code removed still it will work in the same fashion as it used to earlier, thanks to Lombok.
We would have to add a new Class which would help us in generating the fake data on runtime using the Builder Pattern. We would be calling this class as DataBuilder class.
getUserData() method will return the test data required for registering the user using the Java Faker library. Notice the builder() method used after class name RegisterUserWithBuilder. It is appearing because of the @Builder annotation we have used on the top of the RegisterUserWithBuilder class.
After the builder() method we have to pass on the variables we have declared in the RegisterUserWithBuilder class and accordingly pass the fake data that we need to generate for the respective variables.
.firstName (FAKER.name ()
The above piece of code will generate a fake first name and set it in the first name variable. Likewise, we have set fake data in all other variables.
Now, let’s move towards how we use these data in the tests. Its very simple the below code snippet explains it all.
We just need to call the getUserData() method while instantiating the RegisterUserWithBuilder class. Next, we would be calling the Getter methods for the respective variables we declared inside RegisterUserWithBuilder class. Remember we had passed the @Getter annotation on the top of the RegisterUserWithBuilder class, this actually helps in calling the Getter methods here.
Also to mention here, we did not require here to pass on the multiple data as the constructor parameters for the RegisterUserWithBuilder class instead we just instantiated the class and called the getUserData() method!
How easy it is to generate the unique data and pass it on in the tests without having to write multiple lines of boilerplate codes.
Thanks to the Builder Pattern and Lombok!
Running the Test
Let’s run the test and check if the user details get printed in the console. Here is the screenshot of the tests run and we are able to see that the fake data generation worked as a charm without writing multiple lines of code.
Some more example blog links
I have used the same way of generating the fake test data using Builder Pattern with Lombok and Java Faker library in the following blogs where I talk about the end to end testing, checkout the below blog as well as it would give you better understanding of using Builder Pattern with Lombok and Java Faker library:
- How to perform JSON Schema Validation using Rest-Assured?
- Writing Selenium Web Automation tests in Fluent way!
- End to End Testing using Selenium WebDriver and Java
- End to End API Testing using rest-assured
In this blog we discussed How we can make use of Builder Pattern with Lombok and Java Faker library to generate fake test data on run time and use it in our automated tests without writing multiple lines of boilerplate code.
I hope it would help you a lot in reducing the code lines and writing your code in much cleaner way.
Contact me for Paid trainings related to Test Automation and Software Testing, ping me using any of the social media site listed on LinkTree or email me @email@example.com.