Posts

Showing posts with the label Java Annotations

Testing Lombok-based Classes in Java

Testing Lombok-based Classes in Java Lombok simplifies Java by auto-generating boilerplate like getters, setters, and constructors. But how do you **test** classes that use Lombok annotations? Here's a practical guide to unit testing Lombok-powered classes — and when you might want to go beyond relying on Lombok alone. ✅ Lombok Class Example Let’s use this simple class as our example: import lombok.Data; @Data public class User { private String name; private int age; } Lombok generates getName() , setName() , getAge() , setAge() , equals() , hashCode() , and toString() . 🔍 Should You Test Getters and Setters? Generally, you don’t need to test Lombok-generated methods like getters and setters — they’re stable and widely trusted. However, you might want to test: Custom logic added manually Builder pattern usage Correct field population Equality and immutability (e.g., for valu...

Lombok – Reduce Boilerplate in Java

Lombok – Reduce Boilerplate in Java Writing boilerplate code in Java — like getters, setters, constructors, and builders — can get repetitive. Project Lombok simplifies this with annotations that auto-generate code at compile-time, keeping your classes clean and focused. 🔧 Setup To use Lombok, add this dependency to your project: ➡️ Maven <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.30</version> <scope>provided</scope> </dependency> ➡️ Gradle compileOnly 'org.projectlombok:lombok:1.18.30' annotationProcessor 'org.projectlombok:lombok:1.18.30' 📦 Example Without Lombok public class User { private String name; private int age; public User() {} public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } ...