80 lines
2.6 KiB
Plaintext
80 lines
2.6 KiB
Plaintext
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 CreditAccountTest {
|
|
|
|
private static final double epsilon = 0.0005;
|
|
|
|
private CreditAccount sub;
|
|
|
|
@BeforeEach
|
|
public void setUp() {
|
|
sub = new CreditAccount(10_000.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");
|
|
|
|
sub.withdraw(20_000.0);
|
|
assertEquals(-5000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> {
|
|
sub.withdraw(20_000.0);
|
|
}, "Withdrawal exceeding the credit limit should have triggered an IllegalArgumentException");
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Check that the credit limit works as expected")
|
|
public void testCreditLine() {
|
|
assertEquals(10_000.0, sub.getCreditLine(), epsilon, "The credit limit was incorrect");
|
|
|
|
sub.setCreditLine(5000.0);
|
|
assertEquals(5000.0, sub.getCreditLine(), epsilon, "The credit limit was incorrect");
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> {
|
|
sub.setCreditLine(-5000.0);
|
|
}, "Cannot have a negative credit limit");
|
|
|
|
assertEquals(5000.0, sub.getCreditLine(), epsilon, "The credit limit was incorrect");
|
|
|
|
sub.withdraw(4000.0);
|
|
|
|
assertThrows(IllegalStateException.class, () -> {
|
|
sub.setCreditLine(3000.0);
|
|
}, "Cannot set a credit limit that would result in an invalid balance");
|
|
|
|
assertEquals(-4000.0, sub.getBalance(), epsilon, "The balance was incorrect");
|
|
assertEquals(5000.0, sub.getCreditLine(), epsilon, "The credit limit was incorrect");
|
|
}
|
|
}
|