• October 21, 2024
Lombok

When transforming your test cases into automation scripts, some general rules should be followed: code compiled and build, tests successfully passed, understandable flows, and smooth test structure. Many of today’s concepts, design patterns, and libraries help in achieving a stable automation test suite. Specifically, API automation testing at all testing levels is always relying on some of the popular libraries that can ease the process of writing scripts and understand the test flows.

In this post, we are going to talk about some of the main annotations, the very popular Java library called Lombok has that are used to easily create models for manipulation and removing(hiding) boilerplate code.

What is Lombok and why is it used?

Lombok is a Java library that eliminates all of the standard boilerplate code. It affects the models that are created for API testing and uses annotations to achieve this. It can be used for various IDE’s like IntelliJ, Eclipse, Netbeans, etc. Its annotations generate the needed code during compilation, making our code look shorter, simpler, and more understandable. Saving many lines of code is one of the goals of using this library in our test automation framework.

What are the benefits?

Of course, every library comes with many benefits to our code, and Lombok is yet another one that gives us some advantages:

  • Saves time for writing standard boilerplate code
  • Making code more cleaner than before
  • Regular updates means more stable tool
  • Open source library

What is Lombok Builder Annotation?

One of its main annotations is called @Builder annotation.

Lombok Builder is an annotation that creates complex builder APIs for your class. It can be used on a class, method level, or constructor. When we place the Lombok Bulder annotation on a class, that means that Lombok creates a builder for all instances of all fields in that class. When placing the annotation on a method level, the generated builder class includes fields that match the parameters of the method or we can use the Lombok Bulder on a constructor level to create a builder for specific fields.

  • Class level builder: Creates builder for all fields within the class
  • Method-level builder: Creates a builder to match parameters
  • Constructor-level builder: Creates build for specific fields

Basically, we create a specific model using Builder annotation that’s going to be used as a request payload or as a response payload on the API testing level. We’re going to focus on creating a model on a class level since it is mostly utilized for testing RESTful APIs.

What is Lombok Data Annotation?

@Data annotation is another main Lombok annotation. Basically, it is an all-in-one annotation that includes @ToString, @Getter, @Setter, @EqualsAndHashCode, and @RequiredArgsConstructor.

In other words, it is a shorter way of creating simple POJO’s (Plain Old Java Object). So, for every field, @Data creates getter, setter, equals, toString, and hashCode for all of the fields created in the class.

This is how a model class would look using Lombok @Data annotation:

@Data
public void Car {
   public String model;
   public String fuelType;
   public int age;
}

This is how a class would look with Vanilla Java:

public void Car {
   public String model;
   public String fuelType;
   public int age;

  void setModel(String model) {
    this.model= model;
  }
  
  public String getModel() {
    return this.model;
  }

  void setAge(int age) {
    this.age = age;
  }
  
  public int getAge() {
    return this.age;
  }

  public Car() {
  }

@Override
  public String toString() {
    return "Car(model=" + this.getModel() + ", age=" + this.getAge() + ")";
  }

  protected boolean canEqual(final Object other) {
    return other instanceof User1;
  }

  @Override
  public int hashCode() {
    final int PRIME = 59;
    int result = 1;
    final Object $model = this.getModel();
    result = result * PRIME + ($model == null ? 43 :   $model.hashCode());
    final Object $age = this.getAge();
    result = result * PRIME + ($age == null ? 43 : $age.hashCode());
    return result;
  }
}

As you can see, the code lines that we save is pretty large since all of this is done in the background. All we need to do is created the fields we are going to use the leave the rest to @Data annotation.

Creating Models with @Builder and @Data annotations

To create model classes and manipulate with the model we need to create a class where we define the fields we are going to use.

@Builder
@Data
public void Car {
   public String model;
   public String fuelType;
   public int age;
}

Then, we can build the class Car:

Car car = Car.builder()
  .model("BMW")
  .fuelType("Diesel")
  .age("2015)
  .build()

Now, if you want to access some of these fields for assertion purposes, we can write:

assertThat(car.getModel().is(equalTo("BMW")));
assertThat(car.getFuelType().is(equalTo("Diesel")));
assertThat(car.getAge().is(equalTo(2015)));

Conclusion

Creating request or response models has never been so easy when API automation comes into the picture. Lombok’s ability to provide all of the necessary features to reduce our code and look more cleaner and understandable together with its regular community maintenance is another proof of what a powerful Java library can do to our automation test structure.

Share This Post


Latest Posts

Vladimir Simonovski
Vladimir Simonovski

Software Automation Engineer with almost 5 years of experience. Involved in many QA activities for the insurance and banking platforms. Follow QAMIND on Twitter and LinkedIn to get fresh content around Software Testing.