July 22, 2024
Check Information Technology in Automation Testing

I guess you might need come throughout a state of affairs whereas automating API/net or cell functions the place, whereas registering a consumer, chances are you’ll be setting the handle for testing a product within the end-to-end consumer journey in take a look at automation.

So, how do you try this?

Usually, we create a POJO class in Java with the fields required to register a consumer or to set the handle for testing the product after which set the values within the take a look at utilizing the constructor of the POJO class.

Let’s check out an instance of registering a consumer the place the next are obligatory fields required to fill within the registration kind:

  1. First Identify
  2. Final Identify
  3. Deal with
  4. Metropolis
  5. State
  6. Nation
  7. Cellular Quantity

As we have to deal with these fields in automation testing we must go on respective values within the fields on the time of executing the assessments.

Earlier than Utilizing the Builder Sample

A POJO class, with the above-mentioned obligatory fields, can be created with the Getter and Setter strategies inside that POJO class, and utilizing a constructor values are set within the respective fields.

Take a look at the code instance of RegisterUser class given beneath for the illustration of what we’re discussing.

public class RegisterUser 
    personal String firstName;
    personal String lastName;
    personal String handle;
    personal String metropolis;
    personal String state;
    personal String nation;
    personal String mobileNumber;

    public RegisterUser (closing String firstName, closing String lastName, closing String handle, closing String metropolis,
        closing String state, closing String nation, closing String mobileNumber) 
        this.firstName = firstName;
        this.lastName = lastName;
        this.handle = handle;
        this.metropolis = metropolis;
        this.state = state;
        this.nation = nation;
        this.mobileNumber = mobileNumber;

    public String getFirstName () 
        return firstName;

    public void setFirstName (closing String firstName) 
        this.firstName = firstName;

    public String getLastName () 
        return lastName;

    public void setLastName (closing String lastName) 
        this.lastName = lastName;

    public String getAddress () 
        return handle;

    public void setAddress (closing String handle) 
        this.handle = handle;

    public String getCity () 
        return metropolis;

    public void setCity (closing String metropolis) 
        this.metropolis = metropolis;

    public String getState () 
        return state;

    public void setState (closing String state) 
        this.state = state;

    public String getCountry () 
        return nation;

    public void setCountry (closing String nation) 
        this.nation = nation;

    public String getMobileNumber () 
        return mobileNumber;

    public void setMobileNumber (closing String mobileNumber) 
        this.mobileNumber = mobileNumber;

Now, if we need to use this POJO, we must create an occasion of RegisterUser class and go the values within the constructor parameters as given within the code instance beneath to set the information within the respective fields.

Take a look at the beneath instance of the Register Person take a look at of how we do it.

public class RegistrationTest 

    public void testRegisterUser () 
        RegisterUser registerUser = new RegisterUser ("John", "Doe", "302, Adam Road, 1st Lane", "New Orleans",
            "New Jersey", "US", "52145364");

        assertEquals (registerUser.getFirstName (), "John");
        assertEquals (registerUser.getCountry (), "US");

There have been simply seven fields within the instance we took for registering the consumer. Nonetheless, this may not be the case with each software. There could be some extra further fields required and because the fields carry on rising each time, we would wish to replace the POJO class with respective Getter and Setter strategies and likewise replace the parameters within the constructor.

Lastly, we would wish so as to add the values to these fields so the information may very well be handed within the precise area required.

Lengthy story quick, we would wish to replace the code even when there’s a single new area added, additionally, it doesn’t look clear so as to add values as parameters within the assessments.

Fortunately, the Builder Design Sample in Java involves the rescue right here.

available on GitHub

What Is Builder Design Sample in Java?

Builder design sample is a creational design sample that permits you to assemble advanced objects step-by-step. The sample lets you produce differing types and representations of an object utilizing the identical development code. 

Builder Sample helps us clear up the problem of setting the parameters by offering a method to construct the objects step-by-step by offering a technique that returns the ultimate object which can be utilized within the precise assessments.

What Is Lombok?

Challenge Lombok is a Java library that routinely plugs into your editor and builds instruments, spicing up your Java. It’s an annotation-based Java library that helps in lowering the boilerplate code.

It helps us in writing quick and crisp code with out having to put in writing the boilerplate code. Bypassing the @Getterannotation over the category, it routinely generates Getter strategies. Equally, you don’t have to put in writing the code for Setter strategies as nicelyits @Setterannotation up to date over the category routinely generates the Setter strategies.

It additionally has help for utilizing the Builder design sample so we simply must put the @Builderannotation above the category and the remaining can be taken care of by the Lombok library.

To make use of Lombok annotations within the venture we have to add the next Maven dependency:

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->

Utilizing the Builder Design Sample With Lombok

Earlier than we begin refactoring the code we now have written, let me inform you in regards to the DataFaker library in addition to the way it helps in producing pretend knowledge that can be utilized for testing. Ideally, in our instance, each newly registered consumer’s knowledge ought to be distinctive in any other case we could get a reproduction knowledge error and the take a look at will fail.

Right here, the DataFaker library will assist us in offering distinctive knowledge in every take a look at execution thereby serving to us with registering a brand new consumer with distinctive knowledge each time the registration take a look at is run.

To make use of the DataFaker library, we have to add the next Maven dependency to our venture.

<!-- https://mvnrepository.com/artifact/web.datafaker/datafaker -->

Now, let’s begin refactoring the code. First, we are going to make the adjustments to the RegisterUser class. We might be eradicating all of the Getter and Setter strategies and likewise the constructor and including the @Getter and @Builder annotation tags on the highest of the RegisterUser class.

Right here is how the RegisterUser class seems to be now after the refactoring

public class RegisterUserWithBuilder 
    personal String firstName;
    personal String lastName;
    personal String handle;
    personal String metropolis;
    personal String state;
    personal String nation;
    personal String mobileNumber;

How clear and crisp it seems to be with that refactoring being carried out. A number of traces of code are eliminated nonetheless it’ll nonetheless work in the identical trend because it used to earlier, due to Lombok.

We must add a brand new Java class for producing the pretend knowledge on runtime utilizing the Builder design sample. We might be calling this new class the DataBuilder class.

public class DataBuilder 

    personal static closing Faker FAKER = new Faker();

    public static RegisterUserWithBuilder getUserData () 
        return RegisterUserWithBuilder.builder ()
            .firstName (FAKER.title ()
                .firstName ())
            .lastName (FAKER.title ()
                .lastName ())
            .handle (FAKER.handle ()
                .streetAddress ())
            .state (FAKER.handle ()
                .state ())
            .metropolis (FAKER.handle ()
                .metropolis ())
            .nation (FAKER.handle ()
                .nation ())
            .mobileNumber (String.valueOf (FAKER.quantity ()
                .numberBetween (9990000000L, 9999999999L)))
            .construct ();

The getUserData() methodology will return the take a look at knowledge required for registering the consumer utilizing the DataFaker library. Discover the builder() methodology used after the category title RegisterUserWithBuilderIt seems due to the @Builder annotation we now have used on the highest of the RegisterUserWithBuilder class.

After the builder() methodology we now have to go on the variables we now have declared within the RegisterUserWithBuilder class and accordingly, go the pretend knowledge that we have to generate for the respective variables.

RegisterUserWithBuilder.builder ()
    .firstName (FAKER.title ()
	.firstName ());

The above piece of code will generate a pretend first title and set it within the first title variable. Likewise, we now have set pretend knowledge in all different variables.

Now, let’s transfer in direction of how we use these knowledge within the assessments. It is quite simple, the beneath code snippet explains all of it. 

    public void testRegisterUserWithBuilder () 
        RegisterUserWithBuilder registerUserWithBuilder = getUserData ();
        System.out.println (registerUserWithBuilder.getFirstName ());
        System.out.println (registerUserWithBuilder.getLastName ());
        System.out.println (registerUserWithBuilder.getAddress ());
        System.out.println (registerUserWithBuilder.getCity ());
        System.out.println (registerUserWithBuilder.getState ());
        System.out.println (registerUserWithBuilder.getCountry ());
        System.out.println (registerUserWithBuilder.getMobileNumber ());

We simply must name the getUserData() methodology whereas instantiating the RegisterUserWithBuilder classSubsequent, we’d be calling the Getter strategies for the respective variables we declared contained in the RegisterUserWithBuilder class. Keep in mind we had handed the @Getter annotation on the highest of the RegisterUserWithBuilder class, this truly helps in calling the Getter strategies right here.

Additionally, we aren’t required to go on a number of knowledge because the constructor parameters for the RegisterUserWithBuilder class, as an alternative we simply must instantiate the category and name the getUserData() methodology!

How straightforward it’s to generate the distinctive knowledge and go it on within the assessments with out having to put in writing a number of traces of boilerplate codes.

Due to the Builder design sample and Lombok!

Working the Check

Let’s run the take a look at and verify if the consumer particulars get printed within the console. We will see that the pretend knowledge is generated efficiently within the following screenshot of the take a look at execution.

running the test


On this weblog, we mentioned making use of the builder design sample in Java with Lombok and DataFaker library to generate pretend take a look at knowledge on run time and use it in our automated assessments. It might ease the take a look at knowledge era course of by eliminating the necessity to replace the take a look at knowledge earlier than working the assessments.

I hope it’ll assist you numerous in lowering the code traces and writing your code in a a lot cleaner manner.

Glad Testing!