Skip to content

Instantly share code, notes, and snippets.

@mcculley
Last active June 28, 2020 20:38
Show Gist options
  • Save mcculley/3bcdc8f1a40ec6084dba88fbbdec9755 to your computer and use it in GitHub Desktop.
Save mcculley/3bcdc8f1a40ec6084dba88fbbdec9755 to your computer and use it in GitHub Desktop.
Embedded test example
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
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;
}
@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());
}
static {
TestUtilities.runTests(Account.class);
}
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Test;
public class TestUtilities {
private TestUtilities() {
throw new AssertionError("static utility class is not intended to be instantiated");
}
/**
* Execute all of the methods in a supplied class that have a @Test annotation.
*
* @param c the Class to test
* @throws AssertionError if any tests fail
*/
public static void runTests(final Class c) {
try {
for (final Method method : c.getMethods()) {
boolean isTestMethod = method.isAnnotationPresent(Test.class);
final Object o = c.getDeclaredConstructor().newInstance();
if (isTestMethod) {
final Test test = method.getDeclaredAnnotation(Test.class);
try {
method.invoke(o, null);
} catch (final InvocationTargetException ite) {
final Throwable targetException = ite.getTargetException();
if (!test.expected().isInstance(targetException)) {
throw new AssertionError(String.format("unexpected exception type thrown: %s",
targetException.getClass().getName()), targetException);
}
}
}
}
} catch (final Exception e) {
throw new AssertionError(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment