|
| 1 | +package com.codedifferently.lesson17.bank; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | + |
| 8 | +import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.Set; |
| 11 | +import java.util.UUID; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +public class SavingAccountTest { |
| 16 | + private SavingAccount classUnderTest; |
| 17 | + private Set<Customer> owners; |
| 18 | + |
| 19 | + @BeforeEach |
| 20 | + void setUp() { |
| 21 | + owners = new HashSet<>(); |
| 22 | + owners.add(new Customer(UUID.randomUUID(), "John Doe")); |
| 23 | + owners.add(new Customer(UUID.randomUUID(), "Jane Smith")); |
| 24 | + classUnderTest = new SavingAccount("123456789", owners, 100.0); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + void getAccountNumber() { |
| 29 | + assertEquals("123456789", classUnderTest.getAccountNumber()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void getOwners() { |
| 34 | + assertEquals(owners, classUnderTest.getOwners()); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void deposit() { |
| 39 | + classUnderTest.deposit(50.0); |
| 40 | + assertEquals(150.0, classUnderTest.getBalance()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void deposit_withNegativeAmount() { |
| 45 | + assertThatExceptionOfType(IllegalArgumentException.class) |
| 46 | + .isThrownBy(() -> classUnderTest.deposit(-50.0)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void withdraw() { |
| 51 | + classUnderTest.withdraw(50.0); |
| 52 | + assertEquals(50.0, classUnderTest.getBalance()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void withdraw_withNegativeAmount() { |
| 57 | + assertThatExceptionOfType(IllegalStateException.class) |
| 58 | + .isThrownBy(() -> classUnderTest.withdraw(-50.0)) |
| 59 | + .withMessage("Withdrawal amount must be positive"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void withdraw_withInsufficientBalance() { |
| 64 | + assertThatExceptionOfType(InsufficientFundsException.class) |
| 65 | + .isThrownBy(() -> classUnderTest.withdraw(150.0)) |
| 66 | + .withMessage("Account does not have enough funds for withdrawal"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void getBalance() { |
| 71 | + assertEquals(100.0, classUnderTest.getBalance()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void closeAccount_withPositiveBalance() { |
| 76 | + assertThatExceptionOfType(IllegalStateException.class) |
| 77 | + .isThrownBy(() -> classUnderTest.closeAccount()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void isClosed() { |
| 82 | + assertFalse(classUnderTest.isClosed()); |
| 83 | + classUnderTest.withdraw(100); |
| 84 | + classUnderTest.closeAccount(); |
| 85 | + assertTrue(classUnderTest.isClosed()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void equals() { |
| 90 | + SavingAccount otherAccount = new SavingAccount("123456789", owners, 200.0); |
| 91 | + assertEquals(classUnderTest, otherAccount); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void hashCodeTest() { |
| 96 | + SavingAccount otherAccount = new SavingAccount("123456789", owners, 200.0); |
| 97 | + assertEquals(classUnderTest.hashCode(), otherAccount.hashCode()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void toStringTest() { |
| 102 | + String expected = "CheckingAccount{accountNumber='123456789', balance=100.0, isActive=true}"; |
| 103 | + assertEquals(expected, classUnderTest.toString()); |
| 104 | + } |
| 105 | +} |
0 commit comments