lombok

Introduction to Project Lombok. Spice up your Rest Assured project

Photo by Arthur Lambillotte on Unsplash

Productivity tools are a recurring theme in software development. There is so much to be done in short period of time and budget, we could definitely use a productivity hack or two, can’t we?

In this article, I am going to introduce you to one such tool for your Rest Assured project. Well, technically, it is a project, famously called Project Lombok.

What is Project Lombok?

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.

Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

Official Website

If the official introduction of Project Lombok was too heavy for you, let me break it down a little for you.

When you are working on a Java project, you can add Lombok to your IDE as a plugin, then annotate parts of your java program i.e. java class, methods, fields etc. with annotations provided by Lombok and it will automatically generate certain type of code for you during compilation.

What is this certain type of code you ask? At the most basic level, it can generate setters and getters methods for your java class a.k.a. POJOs. However, it does much more than that.

How to use Lombok?

Project Lombok provides a set of annotations that can be attached to java classes, variables, method parameters etc. which will indicate Lombok to add intended code to the program without the programmer having to manually type it. The code is auto-generated. Given the amount of repetitive code programmers have to write, in my opinion the feature is pure magic.

But first, you have to add a maven/gradle dependency and add Lombok plugin to your IDE.

//maven
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>

//gradle
// https://mvnrepository.com/artifact/org.projectlombok/lombok
providedCompile group: 'org.projectlombok', name: 'lombok', version: '1.18.12'

How to add Lombok to Eclipse and Intellij?

Let’s understand this with examples –

Getter and Setter

One of the most common use cases of Lombok is @Getter and @Setter annotations. These annotations are used to auto-generate getter and setter methods in your java class.

So, the following Employee class written with Lombok annotations

Java class with Lombok Getter & Setter annotations
Java class with Lombok Getter & Setter annotations

is equivalent of this –

Java class with code generated by Lombok
Java class with code generated by Lombok

You can add Getter and Setter annotations on individual fields if you would like to generate code selectively. By adding them at class level, you are instructing Lombok to generate getter and setter methods for all the non-static fields of the class.

By default, all the methods will be generated as public. If you need a different access level (public, protected, private etc.), you need to add @Setter(AccessLevel.PROTECTED) in addition to the annotation.

It is important to note that the programmer cannot see the code generated by Lombok i.e screenshot#2. The programmer will only see the version of code illustrated in screenshot#1. And thats the point right? removing the clutter from your code? However, if you would like to see the auto-generated code, you can take the class files and de-compile them.

Constructors

Another important but repetitive piece of code is constructor. Constructors can have no arguments, only required fields and all the fields. Depending on the use case you might need one or all of these constructors. With Lombok, you can add following annotations and respective constructors will be auto-generated upon compilation.

java class with Lombok’s constructor annotations

Some of the libraries like Jackson requires you to have a constructor to map a json string to POJO and these annotations could prove to be very handy in such cases.

I have used another annotation provided by Lombok here – @NotNull.

@NotNull annotation tells Lombok that the annotated field is a required field and hence it should be initialized when invoking the constructor. In addition to this, Lombok generates a null-check statement for you which looks like below code snippet and will be inserted at the very top of your method.

The constructor annotations will auto-generate code which is equivalent to this –

java class with constructor code generated by Lombok
java class with constructor code generated by Lombok

ToString

We define toString() method on a java class to print meaningful information about the object created. In most cases, the toString method prints the properties of the object. With @ToString annotation applied at class level, you don’t need to write code for toString method anymore. By default, all the non-static fields will be printed. If you want to skip certain fields, there are additional annotations that could be used with these fields. You can find detailed documentation on Project Lombok here.

EqualsAndHashcode

Implementations of Equals and Hashcode are required when you are trying to compare two objects.

For example,

if (Employee1.equals(Employee2) {
    System.out.Println('Pass');
}

With Lombok, you can user @EqualsAndHashcode at class level and it will autogenerate corresponding code.

Data

Data is a more generic form of Lombok annotation. It is applied at class level. As you might have realized by now, getter/setter, constructor, toString, equals and hashcode are the most common blocks of code associated with a java class and more often than not you will want to use all of them. Lombok team realizes that as well and hence they have made available @Data annotation that covers all of them -yes, one annotation to rule them all!

@Data annotation will auto-generate code for – @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor!

Builder

Builder as the name suggests adds builder api to your class which helps you to build objects, however, without any effort on your part. If you add @Builder annotation to your java class or constructor, you can use following syntax out of box to construct objects.

java example that uses @Builder annotation from Lombok
java example that uses @Builder annotation from Lombok

Employee.builder() … pattern used in above example is created by Lombok out of the box just because we added @Builder annotation to the employee class. Now that is powerful!

When working with Rest Assured projects, we deal with POJOs all the time. POJOs are the building blocks of the requests and responses. For example, let’s assume that you have an API that lets you create employee by calling an API endpoint. You can use the builder annotation to construct employee object and pass it to your rest assured request like this –

Lombok Builder annotation with Rest Assured
Lombok Builder annotation with Rest Assured

But, the Employee POJO is very basic. In real life projects, you will deal with POJOs that are very complex and often nested. For example, you might want to modify a nested object before sending the request or validate property of a nested object in the response. Let’s add complexity to the employee class.

Complex Employee Class
Complex Employee Class

In the above example, if you want to validate the country property which is inside Address object which is part of Employee class, you can access it with syntax like employee.getAddress().city just by adding @Data annotation without having to explicitly implement any getter/setter methods. It saves effort but it also makes your code clear and concise by removing all the unwanted noise from it.

Conclusion

In this article, we saw how Project Lombok automates the task of adding boiler plate code to your java classes with the help of annotations. It not only gives a boost to developer productivity but it also enhances the readability of your code. However, I have just scratched the surface of Lombok. There are many other annotations to explore and Lombok is highly configurable. I would encourage you to explore the full potential of project Lombok by going through its official documentation and finding solutions for your unique problems. I would love to hear about how you are using Lombok in your project! Add your experience in comments section. If you liked the article, give it a like and subscribe!

Processing…
Success! You're on the list.

Leave a Comment

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