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

时间:2016-04-28 08:48:42

标签: java builder lombok

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

有人可以就如何在方法上使用@Builder进行考试吗?

3 个答案:

答案 0 :(得分:28)

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

答案 1 :(得分:18)

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

答案 2 :(得分:0)

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

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

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