diff --git a/java/testing/junit/build.gradle.kts b/java/testing/junit/build.gradle.kts index b1014199..dbf6d37c 100644 --- a/java/testing/junit/build.gradle.kts +++ b/java/testing/junit/build.gradle.kts @@ -1,10 +1,5 @@ plugins { java - application -} - -tasks.javadoc { - isFailOnError = false } repositories { @@ -19,14 +14,7 @@ dependencies { testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.10.1") } -val mainClass: String by project - -application { - // The following allows to run with: ./gradlew -PmainClass=it.unibo.oop.MyMainClass run - mainClass.set(project.properties["mainClass"].toString()) -} - -val test by tasks.getting(Test::class) { +tasks.withType { // Use junit platform for unit tests useJUnitPlatform() testLogging { diff --git a/java/testing/junit/src/main/java/it/unibo/bank/api/AccountHolder.java b/java/testing/junit/src/main/java/it/unibo/bank/api/AccountHolder.java index 1b0e2eb4..d0609b30 100644 --- a/java/testing/junit/src/main/java/it/unibo/bank/api/AccountHolder.java +++ b/java/testing/junit/src/main/java/it/unibo/bank/api/AccountHolder.java @@ -3,11 +3,17 @@ /** * A BankAccount holder. */ -public class AccountHolder { +public final class AccountHolder { private final String name; private final String surname; private final int id; + /** + * Builds a new {@link AccountHolder}. + * @param name the name of the bank account holder. + * @param surname the surname of the bank account holder. + * @param id the user id of the bank account holder. + */ public AccountHolder(final String name, final String surname, final int id) { this.name = name; this.surname = surname; diff --git a/java/testing/junit/src/main/java/it/unibo/bank/api/BankAccount.java b/java/testing/junit/src/main/java/it/unibo/bank/api/BankAccount.java index 43846cc1..172ea548 100644 --- a/java/testing/junit/src/main/java/it/unibo/bank/api/BankAccount.java +++ b/java/testing/junit/src/main/java/it/unibo/bank/api/BankAccount.java @@ -1,7 +1,7 @@ package it.unibo.bank.api; /** - * Representation of a Banking account + * Representation of a Banking account. */ public interface BankAccount { /** @@ -10,6 +10,7 @@ public interface BankAccount { * computed, it directly collects the amount from the balance. * It also resets the transaction number of the bank account. * The operation is performed only if the user ID is corresponding. + * * @param id the account holder identifier. * @throws IllegalArgumentException if the user ID does not correspond. */ @@ -18,6 +19,7 @@ public interface BankAccount { /** * Increments the number of transactions and adds the amount to the bank account's balance. * The deposit is performed only if the user ID is corresponding. + * * @param id the account holder identifier. * @param amount the amount of money to deposit into the bank account. * @throws IllegalArgumentException if the user ID does not correspond. 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 8d7dd63e..f1a0ea4f 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 @@ -22,12 +22,20 @@ public class SimpleBankAccount implements BankAccount { private double balance; private int transactions; + /** + * Builds a new {@link SimpleBankAccount}. + * @param accountHolder the account holder + * @param balance the initial balance + */ public SimpleBankAccount(final AccountHolder accountHolder, final double balance) { this.holder = accountHolder; this.balance = balance; this.transactions = 0; } + /** + * {@inheritDoc} + */ @Override public void chargeManagementFees(final int id) { if (checkUser(id)) { @@ -38,40 +46,66 @@ public void chargeManagementFees(final int id) { } } + /** + * {@inheritDoc} + */ @Override public void deposit(final int id, final double amount) { this.transactionOp(id, amount); } + /** + * {@inheritDoc} + */ @Override public void depositFromATM(final int id, final double amount) { this.deposit(id, amount - SimpleBankAccount.ATM_TRANSACTION_FEE); } + /** + * {@inheritDoc} + */ @Override public AccountHolder getAccountHolder() { return holder; } + /** + * {@inheritDoc} + */ @Override public double getBalance() { return this.balance; } + /** + * Sets the balance of the bank account. + * + * @param balance the new balance + */ protected void setBalance(final double balance) { this.balance = balance; } + /** + * {@inheritDoc} + */ @Override public int getTransactionsCount() { return this.transactions; } + /** + * {@inheritDoc} + */ @Override public void withdraw(final int id, final double amount) { this.transactionOp(id, -amount); } + /** + * {@inheritDoc} + */ @Override public void withdrawFromATM(final int id, final double amount) { this.withdraw(id, amount + SimpleBankAccount.ATM_TRANSACTION_FEE); 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 790d5d51..60246050 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 @@ -7,8 +7,17 @@ */ public class StrictBankAccount extends SimpleBankAccount { + /** + * Transaction fee charged to the bank account. + */ public static final double TRANSACTION_FEE = 0.1; + /** + * Builds a new {@link StrictBankAccount}. + * + * @param accountHolder the account holder + * @param balance the initial balance + */ public StrictBankAccount(final AccountHolder accountHolder, final double balance) { super(accountHolder, balance); } @@ -20,9 +29,10 @@ public StrictBankAccount(final AccountHolder accountHolder, final double balance * @param usrID the account holder identifier. * @throws IllegalArgumentException if the id does not correspond. */ + @Override public void chargeManagementFees(final int usrID) { final double feeAmount = MANAGEMENT_FEE + getTransactionsCount() * TRANSACTION_FEE; - if (checkUser(usrID) ) { + if (checkUser(usrID)) { if (isWithdrawAllowed(feeAmount)) { setBalance(getBalance() - feeAmount); resetTransactions(); @@ -35,11 +45,13 @@ public void chargeManagementFees(final int usrID) { /** * Takes an amount of money from the bank account. + * * @param usrID the account holder identifier. * @param amount the amount of money to withdraw into the bank account. * @throws IllegalArgumentException if the amount to withdraw is a negative value. * @throws IllegalArgumentException if the balance is lower than the amount to take. */ + @Override public void withdraw(final int usrID, final double amount) { if (amount < 0) { throw new IllegalArgumentException("Cannot withdraw a negative amount"); 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 7415984d..7d219cb0 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 @@ -8,18 +8,25 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -public class TestSimpleBankAccount { +/** + * Test class for the {@link SimpleBankAccount} class. + */ +class TestSimpleBankAccount { private AccountHolder mRossi; private AccountHolder aBianchi; private BankAccount bankAccount; + private static final int AMOUNT = 100; + private static final int ACCEPTABLE_MESSAGE_LENGTH = 10; + /** * Configuration step: this is performed BEFORE each test. */ @BeforeEach - public void setUp() { + void setUp() { this.mRossi = new AccountHolder("Mario", "Rossi", 1); this.aBianchi = new AccountHolder("Andrea", "Bianchi", 2); this.bankAccount = new SimpleBankAccount(mRossi, 0.0); @@ -29,7 +36,7 @@ public void setUp() { * Check that the initialization of the BankAccount is created with the correct values. */ @Test - public void testBankAccountInitialization() { + void testBankAccountInitialization() { assertEquals(0.0, bankAccount.getBalance()); assertEquals(0, bankAccount.getTransactionsCount()); assertEquals(mRossi, bankAccount.getAccountHolder()); @@ -39,29 +46,38 @@ public void testBankAccountInitialization() { * Check that the deposit is performed correctly on the Bank Account. */ @Test - public void testBankAccountDeposit() { - int expectedValue = 0; - assertFalse(bankAccount.getTransactionsCount() > 0); - for(int i = 0; i < 10; i++) { - expectedValue += i * 100; - bankAccount.deposit(mRossi.getUserID(), i * 100); + void testBankAccountDeposit() { + for (int i = 0; i < 10;) { + assertEquals(i, bankAccount.getTransactionsCount()); + assertEquals(i * AMOUNT, bankAccount.getBalance()); + bankAccount.deposit(mRossi.getUserID(), AMOUNT); + i++; + assertEquals(i * AMOUNT, bankAccount.getBalance()); + assertEquals(i, bankAccount.getTransactionsCount()); } - assertEquals(expectedValue, bankAccount.getBalance()); - assertTrue(bankAccount.getTransactionsCount() > 0); } /** * Checks that if the wrong AccountHolder id is given, the deposit will return an IllegalArgumentException. */ @Test - public void testWrongBankAccountDeposit() { + void testWrongBankAccountDeposit() { try { - bankAccount.deposit(aBianchi.getUserID(), 10000); - Assertions.fail(); + bankAccount.deposit(aBianchi.getUserID(), AMOUNT); + Assertions.fail("Depositing from a wrong account was possible, but should have thrown an exception"); } catch (IllegalArgumentException e) { - assertEquals("ID not corresponding: cannot perform transaction", e.getMessage()); + assertEquals(0, bankAccount.getBalance()); // No money was deposited, balance is consistent + assertNotNull(e.getMessage()); // Non-null message + assertFalse(e.getMessage().isBlank()); // Not a blank or empty message + assertTrue(e.getMessage().length() >= ACCEPTABLE_MESSAGE_LENGTH); // A message with a decent length } - // Alternative (with reflection): Assertions.assertThrows + /* + * Conciser alternative + * (once you learn reflection, and preferably after you have learnt lambda expressions): + * Assertions.assertThrows + * + * Use only if you **already** know reflection and lambda expressions. + */ } } 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 c7551d51..659b54d7 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 @@ -7,9 +7,10 @@ import static org.junit.jupiter.api.Assertions.fail; -public class TestStrictBankAccount { - - private final static int INITIAL_AMOUNT = 100; +/** + * Test class for the {@link StrictBankAccount} class. + */ +class TestStrictBankAccount { // Create a new AccountHolder and a StrictBankAccount for it each time tests are executed. private AccountHolder mRossi; diff --git a/java/testing/tdd-deathnote/build.gradle.kts b/java/testing/tdd-deathnote/build.gradle.kts index b1014199..dbf6d37c 100644 --- a/java/testing/tdd-deathnote/build.gradle.kts +++ b/java/testing/tdd-deathnote/build.gradle.kts @@ -1,10 +1,5 @@ plugins { java - application -} - -tasks.javadoc { - isFailOnError = false } repositories { @@ -19,14 +14,7 @@ dependencies { testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.10.1") } -val mainClass: String by project - -application { - // The following allows to run with: ./gradlew -PmainClass=it.unibo.oop.MyMainClass run - mainClass.set(project.properties["mainClass"].toString()) -} - -val test by tasks.getting(Test::class) { +tasks.withType { // Use junit platform for unit tests useJUnitPlatform() testLogging { diff --git a/java/testing/tdd-deathnote/src/main/java/it/unibo/deathnote/api/DeathNote.java b/java/testing/tdd-deathnote/src/main/java/it/unibo/deathnote/api/DeathNote.java index dc294599..eafc1ae5 100644 --- a/java/testing/tdd-deathnote/src/main/java/it/unibo/deathnote/api/DeathNote.java +++ b/java/testing/tdd-deathnote/src/main/java/it/unibo/deathnote/api/DeathNote.java @@ -12,7 +12,7 @@ public interface DeathNote { /** * Returns the list of rules for this DeathNote. */ - static List RULES = List.of( + List RULES = List.of( """ The human whose name is written in this note shall die. """, diff --git a/java/testing/tdd/build.gradle.kts b/java/testing/tdd/build.gradle.kts index b1014199..7e60ac41 100644 --- a/java/testing/tdd/build.gradle.kts +++ b/java/testing/tdd/build.gradle.kts @@ -26,7 +26,7 @@ application { mainClass.set(project.properties["mainClass"].toString()) } -val test by tasks.getting(Test::class) { +tasks.withType { // Use junit platform for unit tests useJUnitPlatform() testLogging {