Last active
June 28, 2020 20:39
-
-
Save mcculley/f24aec40651496a0eab5c72a9fdcd081 to your computer and use it in GitHub Desktop.
Typical JUnit example
This file contains 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
public class Account { | |
public static class InsufficientFundsException extends Exception { | |
} | |
private int balance; | |
public synchronized void transferTo(final int amount, final Account destination) throws InsufficientFundsException { | |
synchronized (destination) { | |
withdraw(amount); | |
destination.deposit(amount); | |
} | |
} | |
public synchronized void withdraw(int amount) throws InsufficientFundsException { | |
if (sufficientFunds(amount)) { | |
balance -= amount; | |
} else { | |
throw new InsufficientFundsException(); | |
} | |
} | |
public synchronized void deposit(int amount) { | |
balance += amount; | |
} | |
public synchronized int balance() { | |
return balance; | |
} | |
private boolean sufficientFunds(int amount) { | |
return balance >= amount; | |
} | |
} |
This file contains 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.Test; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertTrue; | |
public class AccountTest { | |
@Test | |
public void testTransactions() throws Account.InsufficientFundsException { | |
Account account = new Account(); | |
account.deposit(100); | |
assertEquals(100, account.balance()); | |
account.withdraw(60); | |
assertEquals(40, account.balance()); | |
} | |
@Test(expected = Account.InsufficientFundsException.class) | |
public void testFailedTransaction() throws Account.InsufficientFundsException { | |
Account account = new Account(); | |
account.deposit(100); | |
assertEquals(100, account.balance()); | |
account.withdraw(101); | |
} | |
@Test | |
public void testTransfer() throws Account.InsufficientFundsException { | |
Account account1 = new Account(); | |
Account account2 = new Account(); | |
account1.deposit(100); | |
account1.transferTo(100, account2); | |
assertEquals(100, account2.balance()); | |
assertEquals(0, account1.balance()); | |
} | |
@Test | |
public void testFailedTransfer() throws Account.InsufficientFundsException { | |
Account account1 = new Account(); | |
Account account2 = new Account(); | |
account1.deposit(100); | |
boolean caught = false; | |
try { | |
account1.transferTo(101, account2); | |
} catch (final Account.InsufficientFundsException ife) { | |
caught = true; | |
} | |
assertTrue(caught); | |
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