From 7eaa85d524a1e435e8550ff333e8955b319546f4 Mon Sep 17 00:00:00 2001 From: Carlos Cano Date: Sun, 21 Jul 2019 05:20:19 +0200 Subject: [PATCH] add constructor chaining example (#7125) --- .../baeldung/constructors/BankAccount.java | 12 ++++++ .../constructors/ConstructorUnitTest.java | 38 ++++++++++++------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/constructors/BankAccount.java b/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/constructors/BankAccount.java index 3d50e85245..b78b5937e1 100644 --- a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/constructors/BankAccount.java +++ b/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/constructors/BankAccount.java @@ -54,3 +54,15 @@ class BankAccountCopyConstructor extends BankAccount { this.balance = 0.0f; } } + +class BankAccountChainedConstructors extends BankAccount { + public BankAccountChainedConstructors(String name, LocalDateTime opened, double balance) { + this.name = name; + this.opened = opened; + this.balance = balance; + } + + public BankAccountChainedConstructors(String name) { + this(name, LocalDateTime.now(), 0.0f); + } +} diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/constructors/ConstructorUnitTest.java b/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/constructors/ConstructorUnitTest.java index 2cd8832fbf..66c8520e34 100644 --- a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/constructors/ConstructorUnitTest.java +++ b/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/constructors/ConstructorUnitTest.java @@ -1,15 +1,14 @@ package com.baeldung.constructors; -import com.baeldung.constructors.*; +import com.google.common.collect.Comparators; +import org.junit.Test; -import java.util.logging.Logger; import java.time.LocalDateTime; import java.time.Month; +import java.util.ArrayList; +import java.util.logging.Logger; -import org.junit.Test; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatCode; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.*; public class ConstructorUnitTest { final static Logger LOGGER = Logger.getLogger(ConstructorUnitTest.class.getName()); @@ -17,26 +16,28 @@ public class ConstructorUnitTest { @Test public void givenNoExplicitContructor_whenUsed_thenFails() { BankAccount account = new BankAccount(); - assertThatThrownBy(() -> { account.toString(); }).isInstanceOf(Exception.class); + assertThatThrownBy(() -> { + account.toString(); + }).isInstanceOf(Exception.class); } @Test public void givenNoArgumentConstructor_whenUsed_thenSucceeds() { BankAccountEmptyConstructor account = new BankAccountEmptyConstructor(); assertThatCode(() -> { - account.toString(); - }).doesNotThrowAnyException(); + account.toString(); + }).doesNotThrowAnyException(); } @Test public void givenParameterisedConstructor_whenUsed_thenSucceeds() { LocalDateTime opened = LocalDateTime.of(2018, Month.JUNE, 29, 06, 30, 00); BankAccountParameterizedConstructor account = - new BankAccountParameterizedConstructor("Tom", opened, 1000.0f); - + new BankAccountParameterizedConstructor("Tom", opened, 1000.0f); + assertThatCode(() -> { - account.toString(); - }).doesNotThrowAnyException(); + account.toString(); + }).doesNotThrowAnyException(); } @Test @@ -47,7 +48,16 @@ public class ConstructorUnitTest { assertThat(account.getName()).isEqualTo(newAccount.getName()); assertThat(account.getOpened()).isNotEqualTo(newAccount.getOpened()); - + assertThat(newAccount.getBalance()).isEqualTo(0.0f); } + + @Test + public void givenChainedConstructor_whenUsed_thenMaintainsLogic() { + BankAccountChainedConstructors account = new BankAccountChainedConstructors("Tim"); + BankAccountChainedConstructors newAccount = new BankAccountChainedConstructors("Tim", LocalDateTime.now(), 0.0f); + + assertThat(account.getName()).isEqualTo(newAccount.getName()); + assertThat(account.getBalance()).isEqualTo(newAccount.getBalance()); + } }