Last active
July 1, 2020 16:44
-
-
Save mcculley/2c2cc11775ebe5ab1659bbaf2c21789b to your computer and use it in GitHub Desktop.
A unit test for a toy bank account model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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