BAEL-2092 (#5211)
This commit is contained in:
parent
6c45b08fef
commit
e0ec409698
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -1,42 +1,59 @@
|
|||
package com.baeldung.lombok.builder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class BuilderUnitTest
|
||||
{
|
||||
public class BuilderUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenBuilder_WidgetIsBuilt() {
|
||||
Widget testWidget = Widget.builder().name("foo").id(1).build();
|
||||
assertThat(testWidget.getName())
|
||||
.isEqualTo("foo");
|
||||
assertThat(testWidget.getId())
|
||||
.isEqualTo(1);
|
||||
Widget testWidget = Widget.builder()
|
||||
.name("foo")
|
||||
.id(1)
|
||||
.build();
|
||||
assertThat(testWidget.getName()).isEqualTo("foo");
|
||||
assertThat(testWidget.getId()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
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 newWidget = widgetBuilder.id(2).build();
|
||||
assertThat(newWidget.getName())
|
||||
.isEqualTo("foo");
|
||||
assertThat(newWidget.getId())
|
||||
.isEqualTo(2);
|
||||
Widget newWidget = widgetBuilder.id(2)
|
||||
.build();
|
||||
assertThat(newWidget.getName()).isEqualTo("foo");
|
||||
assertThat(newWidget.getId()).isEqualTo(2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void givenBuilderMethod_ClientIsBuilt() {
|
||||
ImmutableClient testImmutableClient = ClientBuilder.builder().name("foo").id(1).build();
|
||||
assertThat(testImmutableClient.getName())
|
||||
.isEqualTo("foo");
|
||||
assertThat(testImmutableClient.getId())
|
||||
.isEqualTo(1);
|
||||
ImmutableClient testImmutableClient = ClientBuilder.builder()
|
||||
.name("foo")
|
||||
.id(1)
|
||||
.build();
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue