Check Knowledge Technology in Automation Testing – DZone – Uplaza

I wager you might need come throughout a situation whereas automating API/internet or cellular purposes the place, whereas registering a person, it’s possible you’ll be setting the tackle for testing a product within the end-to-end person 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 person or to set the tackle 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 person the place the next are necessary fields required to fill within the registration kind:

  1. First Title
  2. Final Title
  3. Handle
  4. Metropolis
  5. State
  6. Nation
  7. Cell Quantity

As we have to deal with these fields in automation testing we should move 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 necessary fields, will likely be created with the Getter and Setter strategies inside that POJO class, and utilizing a constructor values are set within the respective fields.

Try 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 tackle;
    personal String metropolis;
    personal String state;
    personal String nation;
    personal String mobileNumber;

    public RegisterUser (remaining String firstName, remaining String lastName, remaining String tackle, remaining String metropolis,
        remaining String state, remaining String nation, remaining String mobileNumber) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.tackle = tackle;
        this.metropolis = metropolis;
        this.state = state;
        this.nation = nation;
        this.mobileNumber = mobileNumber;
    }

    public String getFirstName () {
        return firstName;
    }

    public void setFirstName (remaining String firstName) {
        this.firstName = firstName;
    }

    public String getLastName () {
        return lastName;
    }

    public void setLastName (remaining String lastName) {
        this.lastName = lastName;
    }

    public String getAddress () {
        return tackle;
    }

    public void setAddress (remaining String tackle) {
        this.tackle = tackle;
    }

    public String getCity () {
        return metropolis;
    }

    public void setCity (remaining String metropolis) {
        this.metropolis = metropolis;
    }

    public String getState () {
        return state;
    }

    public void setState (remaining String state) {
        this.state = state;
    }

    public String getCountry () {
        return nation;
    }

    public void setCountry (remaining String nation) {
        this.nation = nation;
    }

    public String getMobileNumber () {
        return mobileNumber;
    }

    public void setMobileNumber (remaining String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }
}

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

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

public class RegistrationTest {

    @Check
    public void testRegisterUser () {
        RegisterUser registerUser = new RegisterUser ("John", "Doe", "302, Adam Street, 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 person. Nevertheless, this may not be the case with each software. There could be some extra extra fields required and because the fields carry on rising each time, we would want to replace the POJO class with respective Getter and Setter strategies and likewise replace the parameters within the constructor.

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

Lengthy story brief, we would want to replace the code even when there’s a single new subject 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.

What Is Builder Design Sample in Java?

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

Builder Sample helps us remedy the difficulty of setting the parameters by offering a technique 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?

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

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

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

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


    org.projectlombok
    lombok
    1.18.32
    supplied

Utilizing the Builder Design Sample With Lombok

Earlier than we begin refactoring the code we’ve got written, let me let you know concerning the DataFaker library in addition to the way it helps in producing faux knowledge that can be utilized for testing. Ideally, in our instance, each newly registered person’s knowledge ought to be distinctive in any other case we might get a replica 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 person 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 mission.



    internet.datafaker
    datafaker
    2.2.2

Now, let’s begin refactoring the code. First, we are going to make the modifications to the RegisterUser class. We’d 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

@Getter
@Builder
public class RegisterUserWithBuilder {
    personal String firstName;
    personal String lastName;
    personal String tackle;
    personal String metropolis;
    personal String state;
    personal String nation;
    personal String mobileNumber;
}

How clear and crisp it seems to be with that refactoring being finished. A number of strains of code are eliminated nonetheless it would nonetheless work in the identical vogue because it used to earlier, because of Lombok.

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

public class DataBuilder {

    personal static remaining Faker FAKER = new Faker();

    public static RegisterUserWithBuilder getUserData () {
        return RegisterUserWithBuilder.builder ()
            .firstName (FAKER.identify ()
                .firstName ())
            .lastName (FAKER.identify ()
                .lastName ())
            .tackle (FAKER.tackle ()
                .streetAddress ())
            .state (FAKER.tackle ()
                .state ())
            .metropolis (FAKER.tackle ()
                .metropolis ())
            .nation (FAKER.tackle ()
                .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 person utilizing the DataFaker library. Discover the builder() methodology used after the category identify RegisterUserWithBuilderIt seems due to the @Builder annotation we’ve got used on the highest of the RegisterUserWithBuilder class.

After the builder() methodology we’ve got to move on the variables we’ve got declared within the RegisterUserWithBuilder class and accordingly, move the faux knowledge that we have to generate for the respective variables.

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

The above piece of code will generate a faux first identify and set it within the first identify variable. Likewise, we’ve got set faux knowledge in all different variables.

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

    @Check
    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 might be calling the Getter strategies for the respective variables we declared contained in the RegisterUserWithBuilder class. Bear in mind we had handed the @Getter annotation on the highest of the RegisterUserWithBuilder class, this really helps in calling the Getter strategies right here.

Additionally, we’re not required to move 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 move it on within the assessments with out having to jot down a number of strains of boilerplate codes.

Because of the Builder design sample and Lombok!

Working the Check

Let’s run the take a look at and examine if the person particulars get printed within the console. We are able to see that the faux knowledge is generated efficiently within the following screenshot of the take a look at execution.

Conclusion

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

I hope it would assist you a large number in decreasing the code strains and writing your code in a a lot cleaner means.

Blissful Testing!

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version