Files
oops/src/test/java/oving2/AccountTest.java
Andreas Omholt Olsen c7c4780535 Add oving 2
2026-01-19 11:53:18 +01:00

69 lines
1.5 KiB
Java

package oving2;
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 AccountTest {
private static final double epsilon = 0.000001;
private Account account;
@BeforeEach
public void setUp() {
account = new Account(100, 5);
}
@Test
@DisplayName("Private fields")
public void testPrivateFields() {
TestHelper.checkIfFieldsPrivate(Account.class);
}
@Test
public void testConstructor() {
assertEquals(100.0d, account.getBalance(), epsilon);
assertEquals(5.0d, account.getInterestRate(), epsilon);
assertThrows(IllegalArgumentException.class, () -> {
new Account(-1, 5);
});
assertThrows(IllegalArgumentException.class, () -> {
new Account(100, -1);
});
}
@Test
public void testSetInterestRate() {
account.setInterestRate(7);
assertEquals(7, account.getInterestRate(), epsilon);
assertThrows(IllegalArgumentException.class, () -> {
account.setInterestRate(-2);
});
}
@Test
public void testDeposit() {
account.deposit(100);
assertEquals(200.0d, account.getBalance(), epsilon);
assertThrows(IllegalArgumentException.class, () -> {
account.deposit(-50);
});
}
@Test
public void testWithdraw() {
account.withdraw(50);
assertEquals(50.0d, account.getBalance(), epsilon);
assertThrows(IllegalArgumentException.class, () -> {
account.withdraw(150);
});
}
}