From 000ae20d5dcbc1786510439ea3b36afc00589544 Mon Sep 17 00:00:00 2001 From: Maciej Glowka Date: Sun, 3 Oct 2021 22:11:58 +0200 Subject: [PATCH] BAEL-5134: an example of constructor chaining, added unit tests --- .../constructorchaining/Customer.java | 34 +++++++++++++ .../baeldung/constructorchaining/Person.java | 51 +++++++++++++++++++ .../constructorchaining/CustomerUnitTest.java | 31 +++++++++++ .../constructorchaining/PersonUnitTest.java | 29 +++++++++++ 4 files changed, 145 insertions(+) create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/constructorchaining/Customer.java create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/constructorchaining/Person.java create mode 100644 core-java-modules/core-java-lang-4/src/test/java/com/baeldung/constructorchaining/CustomerUnitTest.java create mode 100644 core-java-modules/core-java-lang-4/src/test/java/com/baeldung/constructorchaining/PersonUnitTest.java diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/constructorchaining/Customer.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/constructorchaining/Customer.java new file mode 100644 index 0000000000..62dbdef297 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/constructorchaining/Customer.java @@ -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); + } +} diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/constructorchaining/Person.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/constructorchaining/Person.java new file mode 100644 index 0000000000..02ce2220ba --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/constructorchaining/Person.java @@ -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); + } +} diff --git a/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/constructorchaining/CustomerUnitTest.java b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/constructorchaining/CustomerUnitTest.java new file mode 100644 index 0000000000..ad00ea65b8 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/constructorchaining/CustomerUnitTest.java @@ -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()); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/constructorchaining/PersonUnitTest.java b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/constructorchaining/PersonUnitTest.java new file mode 100644 index 0000000000..8322917951 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/constructorchaining/PersonUnitTest.java @@ -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()); + } +} \ No newline at end of file