如何在方法上使用Lombok @Builder注释

我想有一个简单的方法来构建测试数据,并发现Builder模式非常适合这里描述的。 然而,为了减少组件测试中的样板代码,我发现来自Project Lombok的@Builder是一个不错的候选人。 但是,我无法找到有关如何在方法上使用它的任何文档或在线示例。 我想在某种工厂方法上使用@Builder,因为我无法对实现进行任何更改。

有人可以给出一个关于如何在方法上实际使用@Builder的考试吗?

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()); } } 

这就是你使用@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); } } 

我认为上面的答案完成了答案,但我仍然想在这里指出一些观点,如果你使用构建器,它将生成文档中给出的这7件事。

•在构建器中:目标的每个参数的一个私有非静态非最终字段。 •在构建器中:包私有no-args空构造函数。 •在构建器中:对于目标的每个参数,类似于“setter”的方法:它与该参数和相同名称具有相同的类型。 它返回构建器本身,以便可以链接setter调用,如上例所示。 •在构建器中:一个调用方法的build()方法,传入每个字段。 它返回与目标返回的相同类型。 •在构建器中:一个合理的toString()实现。 •在包含目标的类中:builder()方法,用于创建构建器的新实例。 •名为FooBuilder的内部静态类,具有与静态方法相同的类型参数(称为构建器)。

当你必须编写unit testing用例并且你的组织告诉你不要忽略模型/ pojo时,Lombok会产生一个问题,那时覆盖这些自动生成的方法有点麻烦。