This commit is contained in:
Andrea Ligios 2018-09-09 20:13:53 +02:00 committed by maibin
parent 6c45b08fef
commit e0ec409698
3 changed files with 69 additions and 22 deletions

View File

@ -0,0 +1,19 @@
package com.baeldung.lombok.builder;
import lombok.Builder;
import lombok.Getter;
@Getter
public class Child extends Parent {
private final String childName;
private final int childAge;
@Builder(builderMethodName = "childBuilder")
public Child(String parentName, int parentAge, String childName, int childAge) {
super(parentName, parentAge);
this.childName = childName;
this.childAge = childAge;
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.lombok.builder;
import lombok.Builder;
import lombok.Getter;
@Getter
@Builder
public class Parent {
private final String parentName;
private final int parentAge;
}

View File

@ -1,42 +1,59 @@
package com.baeldung.lombok.builder; package com.baeldung.lombok.builder;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*; public class BuilderUnitTest {
public class BuilderUnitTest
{
@Test @Test
public void givenBuilder_WidgetIsBuilt() { public void givenBuilder_WidgetIsBuilt() {
Widget testWidget = Widget.builder().name("foo").id(1).build(); Widget testWidget = Widget.builder()
assertThat(testWidget.getName()) .name("foo")
.isEqualTo("foo"); .id(1)
assertThat(testWidget.getId()) .build();
.isEqualTo(1); assertThat(testWidget.getName()).isEqualTo("foo");
assertThat(testWidget.getId()).isEqualTo(1);
} }
@Test @Test
public void givenToBuilder_whenToBuilder_BuilderIsCreated() { public void givenToBuilder_whenToBuilder_BuilderIsCreated() {
Widget testWidget = Widget.builder().name("foo").id(1).build(); Widget testWidget = Widget.builder()
.name("foo")
.id(1)
.build();
Widget.WidgetBuilder widgetBuilder = testWidget.toBuilder(); Widget.WidgetBuilder widgetBuilder = testWidget.toBuilder();
Widget newWidget = widgetBuilder.id(2).build(); Widget newWidget = widgetBuilder.id(2)
assertThat(newWidget.getName()) .build();
.isEqualTo("foo"); assertThat(newWidget.getName()).isEqualTo("foo");
assertThat(newWidget.getId()) assertThat(newWidget.getId()).isEqualTo(2);
.isEqualTo(2);
} }
@Test @Test
public void givenBuilderMethod_ClientIsBuilt() { public void givenBuilderMethod_ClientIsBuilt() {
ImmutableClient testImmutableClient = ClientBuilder.builder().name("foo").id(1).build(); ImmutableClient testImmutableClient = ClientBuilder.builder()
assertThat(testImmutableClient.getName()) .name("foo")
.isEqualTo("foo"); .id(1)
assertThat(testImmutableClient.getId()) .build();
.isEqualTo(1); assertThat(testImmutableClient.getName()).isEqualTo("foo");
assertThat(testImmutableClient.getId()).isEqualTo(1);
} }
@Test
public void givenBuilderAtMethodLevel_ChildInheritingParentIsBuilt() {
Child child = Child.childBuilder()
.parentName("Andrea")
.parentAge(38)
.childName("Emma")
.childAge(6)
.build();
assertThat(child.getChildName()).isEqualTo("Emma");
assertThat(child.getChildAge()).isEqualTo(6);
assertThat(child.getParentName()).isEqualTo("Andrea");
assertThat(child.getParentAge()).isEqualTo(38);
}
} }