Merge pull request #11403 from maciejglowka/BAEL-5134

BAEL-5134: an example of constructor chaining, added unit tests
This commit is contained in:
doodoroma 2021-11-04 17:27:07 +01:00 committed by GitHub
commit 41298ffcf7
4 changed files with 145 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package com.baeldung.constructorchaining;
import java.util.Objects;
public class Customer extends Person {
private final String loyaltyCardId;
public Customer(String firstName, String lastName, int age, String loyaltyCardId) {
this(firstName, null, lastName, age, loyaltyCardId);
}
public Customer(String firstName, String middleName, String lastName, int age, String loyaltyCardId) {
super(firstName, middleName, lastName, age);
this.loyaltyCardId = loyaltyCardId;
}
public String getLoyaltyCardId() {
return loyaltyCardId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Customer customer = (Customer) o;
return Objects.equals(loyaltyCardId, customer.loyaltyCardId);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), loyaltyCardId);
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.constructorchaining;
import java.util.Objects;
public class Person {
private final String firstName;
private final String middleName;
private final String lastName;
private final int age;
public Person(String firstName, String lastName, int age) {
this(firstName, null, lastName, age);
}
public Person(String firstName, String middleName, String lastName, int age) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public String getMiddleName() {
return middleName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(firstName, person.firstName) && Objects.equals(middleName, person.middleName) && Objects.equals(lastName, person.lastName);
}
@Override
public int hashCode() {
return Objects.hash(firstName, middleName, lastName, age);
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.constructorchaining;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class CustomerUnitTest {
@Test
public void givenNameLastNameAndAge_whenUsingDedicatedConstructor_shouldInitializeFieldsAndNullifyMiddleName() {
Customer mark = new Customer("Mark", "Johnson", 23, "abcd1234");
assertEquals(23, mark.getAge());
assertEquals("Mark", mark.getFirstName());
assertEquals("Johnson", mark.getLastName());
assertEquals("abcd1234", mark.getLoyaltyCardId());
assertNull(mark.getMiddleName());
}
@Test
public void givenAllFieldsRequired_whenUsingDedicatedConstructor_shouldInitializeAllFields() {
Customer mark = new Customer("Mark", "Andrew", "Johnson", 23, "abcd1234");
assertEquals(23, mark.getAge());
assertEquals("Mark", mark.getFirstName());
assertEquals("Andrew", mark.getMiddleName());
assertEquals("Johnson", mark.getLastName());
assertEquals("abcd1234", mark.getLoyaltyCardId());
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.constructorchaining;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class PersonUnitTest {
@Test
public void givenNameLastNameAndAge_whenUsingDedicatedConstructor_shouldInitializeFieldsAndNullifyMiddleName() {
Person mark = new Person("Mark", "Johnson", 23);
assertEquals(23, mark.getAge());
assertEquals("Mark", mark.getFirstName());
assertEquals("Johnson", mark.getLastName());
assertNull(mark.getMiddleName());
}
@Test
public void givenAllFieldsRequired_whenUsingDedicatedConstructor_shouldInitializeAllFields() {
Person mark = new Person("Mark", "Andrew", "Johnson", 23);
assertEquals(23, mark.getAge());
assertEquals("Mark", mark.getFirstName());
assertEquals("Andrew", mark.getMiddleName());
assertEquals("Johnson", mark.getLastName());
}
}