Skip to content

Instantly share code, notes, and snippets.

@ythirion
Created April 6, 2020 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ythirion/65bc35b9c547f4e887c0a1d0ea15dab9 to your computer and use it in GitHub Desktop.
Save ythirion/65bc35b9c547f4e887c0a1d0ea15dab9 to your computer and use it in GitHub Desktop.
@RunWith(JUnitQuickcheck.class)
public class Account_properties {
@Property
public void balance_should_be_decremented_of_the_amount(
@From(AccountGenerator.class) Account account,
@From(WithdrawGenerator.class) Withdraw withdraw) {
withEnoughMoney(account, withdraw);
withoutReachingMaxWithdrawal(account, withdraw);
Try<Account> debitedAccount = account.withdraw(withdraw);
Assert.assertEquals(
account.getBalance() - withdraw.getAmount(),
debitedAccount.get().getBalance(), 0);
}
@Property
public void balance_should_be_decremented_even_when_insufficient_balance_because_overdraft_is_authorized(
@From(AccountGenerator.class) Account account,
@From(WithdrawGenerator.class) Withdraw withdraw) {
withInsufficientBalance(account, withdraw);
withoutReachingMaxWithdrawal(account, withdraw);
withOverdraftAuthorized(account);
Try<Account> debitedAccount = account.withdraw(withdraw);
Assert.assertEquals(
account.getBalance() - withdraw.getAmount(),
debitedAccount.get().getBalance(), 0);
}
@Property
public void withdraw_should_not_be_allowed_when_withdraw_over_maxWithdrawal_requested(
@From(AccountGenerator.class) Account account,
@From(WithdrawGenerator.class) Withdraw withdraw) {
assumeThat(account.getMaxWithdrawal(), lessThan(withdraw.getAmount()));
Throwable failure = account.withdraw(withdraw).getCause();
Assert.assertTrue(failure instanceof IllegalArgumentException);
Assert.assertTrue(failure.getMessage().startsWith("Amount exceeding your limit of"));
}
@Property
public void withdraw_should_not_be_allowed_when_no_money_and_no_overdraft(
@From(AccountGenerator.class) Account account,
@From(WithdrawGenerator.class) Withdraw withdraw) {
withInsufficientBalance(account, withdraw);
withoutReachingMaxWithdrawal(account, withdraw);
withoutOverdraftAuthorized(account);
Throwable failure = account.withdraw(withdraw).getCause();
Assert.assertTrue(failure instanceof IllegalArgumentException);
Assert.assertTrue(failure.getMessage().startsWith("Insufficient balance to withdraw"));
}
private void withoutOverdraftAuthorized(Account account) {
assumeThat(account.isOverdraftAuthorized(), is(false));
}
private void withEnoughMoney(Account account, Withdraw withdraw) {
assumeThat(account.getBalance(), greaterThan(withdraw.getAmount()));
}
private void withoutReachingMaxWithdrawal(Account account, Withdraw withdraw) {
assumeThat(account.getMaxWithdrawal(), greaterThan(withdraw.getAmount()));
}
private void withInsufficientBalance(Account account, Withdraw withdraw) {
assumeThat(account.getBalance(), lessThan(withdraw.getAmount()));
}
private void withOverdraftAuthorized(Account account) {
assumeThat(account.isOverdraftAuthorized(), is(true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment