Felix Grund
Instructor @ UBC
Throwing them in our tests doesn't make much sense
We should catch them!
This way we can also "expect" exceptions
/*
* Withdraw money from the account
* MODIFIES: this
* EFFECTS: amount is withdrawn from account and updated
* balance is returned
* throws InsufficientFundsException if balance<amount
* throws NegativeAmountException if amount<0
*/
public double withdraw(double amount)
throws InsufficientFundsException, NegativeAmountException {
public double withdraw(double amount)
throws InsufficientFundsException, NegativeAmountException {
double initialBalance = balance;
if (amount < 0) {
throw new NegativeAmountException();
}
if (amount > getBalance()) {
throw new InsufficientFundsException();
}
balance = balance - amount;
return balance;
}
1
2
@Test
void testWithdrawNoException() {
try {
testAccount.withdraw(150.50);
} catch (NegativeAmountException e) {
fail("Unexpected NegativeAmountException");
} catch (InsufficientFundsException e) {
fail("Unexpected InsufficientFundsException");
}
assertEquals(349.50, testAccount.getBalance());
}
@Test
void testWithdrawNoException() {
try {
testAccount.withdraw(1000);
} catch (InsufficientFundsException e) {
fail("Unexpected InsufficientFundsException");
} catch (NegativeAmountException e) {
fail("Unexpected NegativeAmountException");
}
assertEquals(349.50, testAccount.getBalance());
}
@BeforeEach
void runBefore() {
testAccount = new Account("Jane", 500.0);
}
@Test
void testWithdrawInsufficientFundsException() {
try {
testAccount.withdraw(1000);
fail("InsufficientFundsException was not thrown!");
} catch (NegativeAmountException e) {
fail("Unexpected NegativeAmountException");
} catch (InsufficientFundsException e) {
// all good!
}
assertEquals(500, testAccount.getBalance());
}
@BeforeEach
void runBefore() { testAccount = new Account("Jane", 500.0); }
@Test
void testWithdrawNegativeAmountException() {
try {
testAccount.withdraw(-500);
fail("NegativeAmountException was not thrown!");
} catch (NegativeAmountException e) {
// all good!
} catch (InsufficientFundsException e) {
fail("Unexpected InsufficientFundsException");
}
assertEquals(500, testAccount.getBalance());
}
@BeforeEach
void runBefore() { testAccount = new Account("Jane", 500.0); }
if (amount < 0) {
throw new NegativeAmountException();
}
if (amount > getBalance()) {
throw new InsufficientFundsException();
}
if (amount % 10 != 0) {
throw new NotMultipleOfTenException();
}
@BeforeEach
void runBefore() { testAccount = new Account("Jane", 500.0); }
Assume we had this code
// MODIFIES: this
// EFFECTS: if numThings > 10 throws StressedOutException, otherwise
// does numThings things and returns the value of numThings
int doThings(int numThings) throws StressedOutException;
@Test
public void do3ThingsTestExpectReturn3() {
int numThingsDone = 0;
try {
numThingsDone = doer.doThings(3);
//POINT F
} catch (StressedOutException soe) {
//POINT A
}
//POINT B
}
@Test
public void do11ThingsTestExpectStressedOut() {
int numThingsDone = 0;
try {
numThingsDone = doer.doThings(11);
//POINT E
} catch (StressedOutException soe) {
//POINT C
}
//POINT D
}
No exception expected!
Exception
expected!
assertEquals(3, numThingsDone);
assertEquals(11, numThingsDone);
By Felix Grund