diff --git a/java/testing/junit/src/main/java/it/unibo/bank/impl/SimpleBankAccount.java b/java/testing/junit/src/main/java/it/unibo/bank/impl/SimpleBankAccount.java index 698b843b..9ca85103 100644 --- a/java/testing/junit/src/main/java/it/unibo/bank/impl/SimpleBankAccount.java +++ b/java/testing/junit/src/main/java/it/unibo/bank/impl/SimpleBankAccount.java @@ -5,8 +5,8 @@ public class SimpleBankAccount implements BankAccount { - protected static final double ATM_TRANSACTION_FEE = 1; - protected static final double MANAGEMENT_FEE = 5; + public static final double ATM_TRANSACTION_FEE = 1; + public static final double MANAGEMENT_FEE = 5; private final AccountHolder holder; private double balance; diff --git a/java/testing/junit/src/main/java/it/unibo/bank/impl/StrictBankAccount.java b/java/testing/junit/src/main/java/it/unibo/bank/impl/StrictBankAccount.java index 8ab9a80c..7c0c3c33 100644 --- a/java/testing/junit/src/main/java/it/unibo/bank/impl/StrictBankAccount.java +++ b/java/testing/junit/src/main/java/it/unibo/bank/impl/StrictBankAccount.java @@ -4,7 +4,7 @@ public class StrictBankAccount extends SimpleBankAccount { - private static final double TRANSACTION_FEE = 0.1; + public static final double TRANSACTION_FEE = 0.1; public StrictBankAccount(final AccountHolder accountHolder, final double balance) { super(accountHolder, balance); @@ -16,15 +16,18 @@ public void chargeManagementFees(final int usrID) { setBalance(getBalance() - feeAmount); resetTransactions(); } else { - throw new IllegalArgumentException("ID not corresponding: cannot charge management fees."); + throw new IllegalArgumentException("ID not corresponding: cannot charge management fees"); } } public void withdraw(final int usrID, final double amount) { + if (amount < 0) { + throw new IllegalArgumentException("Cannot withdraw a negative amount"); + } if (isWithdrawAllowed(amount)) { super.withdraw(usrID, amount); } else { - throw new IllegalStateException("Not enough balance: cannot withdraw."); + throw new IllegalArgumentException("Insufficient balance"); } } diff --git a/java/testing/junit/src/test/java/it/unibo/bank/impl/TestSimpleBankAccount.java b/java/testing/junit/src/test/java/it/unibo/bank/impl/TestSimpleBankAccount.java index 6ea7580c..f42331f0 100644 --- a/java/testing/junit/src/test/java/it/unibo/bank/impl/TestSimpleBankAccount.java +++ b/java/testing/junit/src/test/java/it/unibo/bank/impl/TestSimpleBankAccount.java @@ -57,6 +57,7 @@ public void testWrongBankAccountDeposit() { } catch (IllegalArgumentException e) { Assertions.assertEquals("ID not corresponding: cannot perform transaction.", e.getMessage()); } + // Alternative (with reflection): Assertions.assertThrows } } diff --git a/java/testing/junit/src/test/java/it/unibo/bank/impl/TestStrictBankAccount.java b/java/testing/junit/src/test/java/it/unibo/bank/impl/TestStrictBankAccount.java index 1877d442..1c424be4 100644 --- a/java/testing/junit/src/test/java/it/unibo/bank/impl/TestStrictBankAccount.java +++ b/java/testing/junit/src/test/java/it/unibo/bank/impl/TestStrictBankAccount.java @@ -1,13 +1,69 @@ package it.unibo.bank.impl; +import it.unibo.bank.api.AccountHolder; +import it.unibo.bank.api.BankAccount; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static it.unibo.bank.impl.SimpleBankAccount.*; +import static it.unibo.bank.impl.SimpleBankAccount.ATM_TRANSACTION_FEE; +import static it.unibo.bank.impl.StrictBankAccount.TRANSACTION_FEE; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class TestStrictBankAccount { + private final static int INITIAL_AMOUNT = 100; + // 1. Create a new AccountHolder and a StrictBankAccount for it each time tests are executed. + private AccountHolder mRossi; + private BankAccount bankAccount; + + @BeforeEach + public void setUp() { + mRossi = new AccountHolder("Mario", "Rossi", 1); + bankAccount = new StrictBankAccount(mRossi, 0.0); + } // 2. Test the initial state of the StrictBankAccount + @Test + public void testInitialization() { + assertEquals(0.0, bankAccount.getBalance()); + assertEquals(0, bankAccount.getTransactionsCount()); + assertEquals(mRossi, bankAccount.getAccountHolder()); + } + - // 3. Perform a deposit of 100€ and check that the correct amount of management fees is applied to this BankAccount. + // 3. Perform a deposit of 100€, compute the management fees, and check that the balance is correctly reduced. + @Test + public void testManagementFees() { + bankAccount.deposit(mRossi.getUserID(), INITIAL_AMOUNT); + assertEquals(INITIAL_AMOUNT, bankAccount.getBalance()); + bankAccount.chargeManagementFees(mRossi.getUserID()); + assertEquals(INITIAL_AMOUNT - TRANSACTION_FEE - MANAGEMENT_FEE, bankAccount.getBalance()); + } - // 4. Test the withdraw of a negative value + // 4. Test the withdraw of a negative value + @Test + public void testNegativeWithdraw() { + try { + bankAccount.withdraw(mRossi.getUserID(), -INITIAL_AMOUNT); + } catch (IllegalArgumentException e) { + assertNotNull(e.getMessage()); + assertTrue(e.getMessage().length() > 1); + } + } + // 4. Test withdrawing more money than it is in the account + @Test + public void testWithdrawingTooMuch() { + try { + bankAccount.withdraw(mRossi.getUserID(), INITIAL_AMOUNT); + } catch (IllegalArgumentException e) { + assertNotNull(e.getMessage()); + assertTrue(e.getMessage().length() > 1); + } + } }