parent
4d1c8634fa
commit
56a832273e
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.lombok.builder;
|
||||
package com.baeldung.lombok.builder.inheritance.buildermethodname;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.lombok.builder;
|
||||
package com.baeldung.lombok.builder.inheritance.buildermethodname;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.lombok.builder.inheritance.buildermethodname;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class Student extends Child {
|
||||
|
||||
private final String schoolName;
|
||||
|
||||
@Builder(builderMethodName = "studentBuilder")
|
||||
public Student(String parentName, int parentAge, String childName, int childAge, String schoolName) {
|
||||
super(parentName, parentAge, childName, childAge);
|
||||
this.schoolName = schoolName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.lombok.builder.inheritance.superbuilder;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@Getter
|
||||
@SuperBuilder(toBuilder = true)
|
||||
public class Child extends Parent {
|
||||
private final String childName;
|
||||
private final int childAge;
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.lombok.builder.inheritance.superbuilder;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@Getter
|
||||
@SuperBuilder(toBuilder = true)
|
||||
public class Parent {
|
||||
private final String parentName;
|
||||
private final int parentAge;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.lombok.builder.inheritance.superbuilder;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@Getter
|
||||
@SuperBuilder(toBuilder = true)
|
||||
public class Student extends Child {
|
||||
private final String schoolName;
|
||||
}
|
|
@ -40,20 +40,4 @@ public class BuilderUnitTest {
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.lombok.builder.inheritance.buildermethodname;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BuilderInheritanceUsingMethodNameUnitTest {
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSuperBuilderOnAllThreeLevels_StudentInheritingChildAndParentIsBuilt() {
|
||||
Student student = Student.studentBuilder()
|
||||
.parentName("Andrea")
|
||||
.parentAge(38)
|
||||
.childName("Emma")
|
||||
.childAge(6)
|
||||
.schoolName("Baeldung High School")
|
||||
.build();
|
||||
|
||||
assertThat(student.getChildName()).isEqualTo("Emma");
|
||||
assertThat(student.getChildAge()).isEqualTo(6);
|
||||
assertThat(student.getParentName()).isEqualTo("Andrea");
|
||||
assertThat(student.getParentAge()).isEqualTo(38);
|
||||
assertThat(student.getSchoolName()).isEqualTo("Baeldung High School");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
package com.baeldung.lombok.builder.inheritance.superbuilder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BuilderInheritanceUsingSuperBuilderUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSuperBuilderOnParentAndOnChild_ChildInheritingParentIsBuilt() {
|
||||
Child child = Child.builder()
|
||||
.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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSuperBuilderOnParent_StandardBuilderIsBuilt() {
|
||||
Parent parent = Parent.builder()
|
||||
.parentName("Andrea")
|
||||
.parentAge(38)
|
||||
.build();
|
||||
|
||||
assertThat(parent.getParentName()).isEqualTo("Andrea");
|
||||
assertThat(parent.getParentAge()).isEqualTo(38);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenToBuilderIsSetToTrueOnParentAndChild_DeepCopyViaBuilderIsPossible() {
|
||||
Child child1 = Child.builder()
|
||||
.parentName("Andrea")
|
||||
.parentAge(38)
|
||||
.childName("Emma")
|
||||
.childAge(6)
|
||||
.build();
|
||||
|
||||
Child child2 = child1.toBuilder()
|
||||
.childName("Anna")
|
||||
.build();
|
||||
|
||||
assertThat(child2.getChildName()).isEqualTo("Anna");
|
||||
assertThat(child2.getChildAge()).isEqualTo(6);
|
||||
assertThat(child2.getParentName()).isEqualTo("Andrea");
|
||||
assertThat(child2.getParentAge()).isEqualTo(38);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSuperBuilderOnAllThreeLevels_StudentInheritingChildAndParentIsBuilt() {
|
||||
Student student = Student.builder()
|
||||
.parentName("Andrea")
|
||||
.parentAge(38)
|
||||
.childName("Emma")
|
||||
.childAge(6)
|
||||
.schoolName("Baeldung High School")
|
||||
.build();
|
||||
|
||||
assertThat(student.getChildName()).isEqualTo("Emma");
|
||||
assertThat(student.getChildAge()).isEqualTo(6);
|
||||
assertThat(student.getParentName()).isEqualTo("Andrea");
|
||||
assertThat(student.getParentAge()).isEqualTo(38);
|
||||
assertThat(student.getSchoolName()).isEqualTo("Baeldung High School");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenToBuilderIsSetToTrueOnParentChildAndStudent_DeepCopyViaBuilderIsPossible() {
|
||||
Student student1 = Student.builder()
|
||||
.parentName("Andrea")
|
||||
.parentAge(38)
|
||||
.childName("Emma")
|
||||
.childAge(6)
|
||||
.schoolName("School 1")
|
||||
.build();
|
||||
|
||||
Student student2 = student1.toBuilder()
|
||||
.childName("Anna")
|
||||
.schoolName("School 2")
|
||||
.build();
|
||||
|
||||
assertThat(student2.getChildName()).isEqualTo("Anna");
|
||||
assertThat(student2.getChildAge()).isEqualTo(6);
|
||||
assertThat(student2.getParentName()).isEqualTo("Andrea");
|
||||
assertThat(student2.getParentAge()).isEqualTo(38);
|
||||
assertThat(student2.getSchoolName()).isEqualTo("School 2");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue