如何在方法上使用Lombok @Builder注释 [英] How to use Lombok @Builder annotation on Methods
问题描述
我希望有一种简单的方法来构建测试数据,并且发现Builder模式非常适合此处。但是,为了减少组件测试中的样板代码,我发现了来自Project Lombok的 @Builder 一个不错的候选人尝试。但是,我无法找到有关如何在方法上使用它的任何文档或在线示例。我想在某种工厂方法上使用@Builder,因为我无法对实现进行任何更改。
I want to have an easy way to construct test data and have found the Builder pattern to be a good fit as described here. However to reduce boilerplate codes in the component tests even more I have found @Builder from Project Lombok to be a nice candidate to try. However I cant find any documentation or online examples on how to use it on a method. I want to use @Builder on some sort of factory method since I cant make any changes to the implementation.
有人可以给出关于如何实际使用@Builder的考试一个方法?
Can someone give an examle on how to actualy use @Builder on a method?
推荐答案
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());
}
}
这篇关于如何在方法上使用Lombok @Builder注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!