66 lines
2.2 KiB
Java
66 lines
2.2 KiB
Java
package oving7.abstractaccount;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.DisplayName;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
public class SavingsAccountTest {
|
|
|
|
private static final double epsilon = 0.0005;
|
|
|
|
private SavingsAccount sub;
|
|
|
|
@BeforeEach
|
|
public void setUp() {
|
|
sub = new SavingsAccount(1, 50.0);
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Check that deposits work as expected")
|
|
public void testDeposit() {
|
|
assertEquals(0.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
|
|
|
sub.deposit(10_000.0);
|
|
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> {
|
|
sub.deposit(-10_000.0);
|
|
}, "Negative deposit should have triggered an IllegalArgumentException!");
|
|
|
|
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Check that withdrawals work as expected")
|
|
public void testWithdraw() {
|
|
sub.deposit(20_000.0);
|
|
sub.withdraw(5000.0);
|
|
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> {
|
|
sub.withdraw(-10_000.0);
|
|
}, "Negative withdrawal should have triggered an IllegalArgumentException!");
|
|
|
|
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> {
|
|
sub.withdraw(20_000.0);
|
|
}, "Withdrawal exceeding the balance should have triggered an IllegalArgumentException");
|
|
|
|
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
|
|
|
sub.withdraw(10_000.0);
|
|
assertEquals(4950.0, sub.getBalance(), epsilon,
|
|
"The account balance was incorrect after the fee was deducted");
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> {
|
|
sub.withdraw(4930.0);
|
|
}, "Withdrawal exceeding the balance + fee should have triggered an IllegalArgumentException");
|
|
|
|
assertEquals(4950.0, sub.getBalance(), epsilon,
|
|
"The account balance was incorrect after the fee was deducted");
|
|
}
|
|
}
|