Understanding Lombok for Java in 3 minutes

Vicky Ivanova
3 min readAug 13, 2023

What is Lombok?

In Java development, boilerplate code is a common pain point. Whether writing getters and setters or implementing equals() and hashCode() methods, repetitive code can clutter your classes and make them harder to maintain. That’s where Lombok comes into play.

Project Lombok is a Java library that helps developers eliminate much of the boilerplate code by introducing simple annotations. It acts as a code preprocessor during the compilation phase, replacing annotations with the corresponding code. The result is a more concise, readable, and maintainable codebase.

Key Features and Annotations

Lombok offers a variety of features that make common programming tasks in Java more streamlined. Here are some of the key annotations that you might find useful:

  1. @Getter and @Setter:
    These annotations automatically generate getter and setter methods for your fields, allowing you to interact with them without manually writing these methods.
  2. @ToString:
    With this annotation, you can automatically create a toString() method that includes all the fields in your class.
  3. @EqualsAndHashCode:
    This annotation generates equals() and hashCode() methods, considering all non-static fields.
  4. @NoArgsConstructor, @RequiredArgsConstructor, and @AllArgsConstructor:
    These annotations generate constructors with different levels of arguments, reducing the need to write them manually.
  5. @Data:
    A comprehensive annotation that includes @Getter, @Setter, @ToString, @EqualsAndHashCode, and a constructor, essentially bundling the most common functionalities in one annotation.
  6. @Builder:
    Enables the builder pattern for your class, allowing you to create more readable and flexible instances.
  7. @SneakyThrows:
    Allows you to handle checked exceptions without declaring them in your method signature.

These are just a few examples of what Lombok offers. There are many more annotations and capabilities that can be explored based on your specific needs.

Setting up Lombok in the IntelliJ IDEA

Integrating Lombok into your IntelliJ IDEA is a straightforward process. Follow these steps to get started:

1. Add Lombok as a Dependency:

Include Lombok in your project by adding the following Maven dependency:

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version> <! - Use the latest version >
<scope>provided</scope>
</dependency>

2. Install the Lombok Plugin:

In IntelliJ IDEA, go to File -> Settings (or IntelliJ IDEA -> Preferences on a Mac) -> Plugins, and search for "Lombok." Click the "Install" button next to the plugin, and restart IntelliJ IDEA to activate it.

3. Enable Annotation Processing:

Navigate to File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors, and check the box for "Enable annotation processing."

Now, Lombok is fully configured in your IntelliJ IDEA, and you can start using its annotations in your Java classes.

Example of Reducing Code with @Getter, @Setter, etc.

Lombok annotations like @Getter, @Setter, @AllArgsConstructor, and others can dramatically reduce the amount of boilerplate code in your test classes. Here's a before-and-after example to demonstrate this:

public class User {
private String name;
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

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

// Additional boilerplate code for constructors, equals, hashCode, etc.
}

With Lombok:

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class User {
private String name;
private int age;
}

Enhancing Code Readability

The use of Lombok doesn’t just minimize code; it significantly enhances code readability. Here are some ways Lombok can achieve this:

  1. Removing Noise: By eliminating repetitive getter and setter methods, constructors, and other boilerplate code, Lombok makes the class definition more concise, focusing on what truly matters.
  2. Encouraging Immutability: With annotations like @Value, Lombok facilitates the creation of immutable classes, leading to more robust code.
  3. Simplifying Complex Patterns: Annotations like @Builder enable complex patterns with less code, making it easier to understand the class's intent.
  4. Improving Test Data Creation: In test classes, you can leverage Lombok to create test data objects quickly and cleanly, making the test setup more transparent.

Conclusion

Lombok is a powerful tool that significantly reduces boilerplate code in Java projects. You can write more concise and maintainable code by understanding its key features and setting it up in your development environment. Experiment with its various annotations and see how they can transform your codebase.

Lombok logo

I hope this article helps you get started with Lombok! Feel free to add any additional insights or examples in the comments.

Additional Resources

Transforming your Test Automation experience with Lombok: Practical Examples

--

--

Vicky Ivanova

Lead Test Engineer | Test automation | Certified Tester, ISTQB