How to use Lombok @Builder annotation for methods

I need an easy way to build test data, and I found that the Builder pattern works well, as described here . However, to further reduce the standard codes in component tests, I found @Builder from Project Lombok a good candidate to try. However, I cannot find documentation or examples on the Internet on how to use it in a method. I want to use @Builder on some kind of factory method, since I cannot make any changes to the implementation.

Can someone give an example of how to actually use @Builder for a method?

+16
java builder lombok
source share
2 answers
 import static org.junit.Assert.*; import lombok.Builder; import lombok.Value; import org.junit.Test; @SuppressWarnings("javadoc") public class ImmutableAnimals { @Builder(builderMethodName = "dogBuilder") public static Dog newDog(String color, String barkSound) { return new Dog(color, barkSound); } @Builder(builderMethodName = "catBuilder") public static Cat newCat(String color, String meowSound) { return new Cat(color, meowSound); } public static interface Animal { String getColor(); } @Value public static class Cat implements Animal { String color; String meowSound; } @Value public static class Dog implements Animal { String color; String barkSound; } @Test public void testDog() { final String expectedBarkSound = "woof"; final String expectedColor = "brown"; final Dog dog = Animals.dogBuilder() .barkSound(expectedBarkSound) .color(expectedColor) .build(); assertEquals(expectedBarkSound, dog.getBarkSound()); assertEquals(expectedColor, dog.getColor()); } @Test public void testCat() { final String expectedMeowSound = "purr"; final String expectedColor = "white"; final Cat cat = Animals.catBuilder() .meowSound(expectedMeowSound) .color(expectedColor) .build(); assertEquals(expectedMeowSound, cat.getMeowSound()); assertEquals(expectedColor, cat.getColor()); } } 
 import static org.junit.Assert.*; import lombok.Builder; import lombok.Data; import org.junit.Test; @SuppressWarnings("javadoc") public class MutableAnimals { @Builder(builderMethodName = "dogBuilder") public static Dog newDog(String color, String barkSound) { final Dog dog = new Dog(); dog.setBarkSound(barkSound); dog.setColor(color); return dog; } @Builder(builderMethodName = "catBuilder") public static Cat newCat(String color, String meowSound) { final Cat cat = new Cat(); cat.setMeowSound(meowSound); cat.setColor(color); return cat; } public static interface Animal { String getColor(); } @Data public static class Cat implements Animal { String color; String meowSound; } @Data public static class Dog implements Animal { String color; String barkSound; } @Test public void testDog() { final String expectedBarkSound = "woof"; final String expectedColor = "brown"; final Dog dog = MutableAnimals.dogBuilder() .barkSound(expectedBarkSound) .color(expectedColor) .build(); assertEquals(expectedBarkSound, dog.getBarkSound()); assertEquals(expectedColor, dog.getColor()); } @Test public void testCat() { final String expectedMeowSound = "purr"; final String expectedColor = "white"; final Cat cat = MutableAnimals.catBuilder() .meowSound(expectedMeowSound) .color(expectedColor) .build(); assertEquals(expectedMeowSound, cat.getMeowSound()); assertEquals(expectedColor, cat.getColor()); } } 
+19
source share

This is how you use @Builder .

 //Employee.Java import lombok.Builder; import lombok.ToString; @Builder @ToString public class Employee { private final String empName; private final int salary; } // Main.java public class Main { public static void main(String[] args) { Employee emp = Employee.builder().empName("Deendaya").salary(100).build(); System.out.println(emp); } } 
+31
source share

All Articles