Skip to content

Instantly share code, notes, and snippets.

@mcculley
Last active July 1, 2020 16:44
Show Gist options
  • Save mcculley/2c2cc11775ebe5ab1659bbaf2c21789b to your computer and use it in GitHub Desktop.
Save mcculley/2c2cc11775ebe5ab1659bbaf2c21789b to your computer and use it in GitHub Desktop.
A unit test for a toy bank account model
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class AccountTest {
@Test
public void testTransactions() throws Account.InsufficientFundsException {
final Account account = new Account();
account.deposit(100);
assertEquals(100, account.balance());
account.withdraw(60);
assertEquals(40, account.balance());
}
@Test
public void testFailedTransaction() {
final Account account = new Account();
account.deposit(100);
assertEquals(100, account.balance());
assertThrows(Account.InsufficientFundsException.class, () -> account.withdraw(101));
}
@Test
public void testTransfer() throws Account.InsufficientFundsException {
final Account account1 = new Account();
final Account account2 = new Account();
account1.deposit(100);
account1.transferTo(100, account2);
assertEquals(100, account2.balance());
assertEquals(0, account1.balance());
}
@Test
public void testFailedTransfer() {
final Account account1 = new Account();
final Account account2 = new Account();
account1.deposit(100);
assertThrows(Account.InsufficientFundsException.class, () -> account1.transferTo(101, account2));
assertEquals(0, account2.balance());
assertEquals(100, account1.balance());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment