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;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue