lots of exercises in java... from https://github.com/exercism/java

BankAccountTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import org.junit.Before;
  2. import org.junit.Ignore;
  3. import org.junit.Rule;
  4. import org.junit.Test;
  5. import org.junit.rules.ExpectedException;
  6. import java.util.Random;
  7. import static org.junit.Assert.assertEquals;
  8. import static org.junit.Assert.fail;
  9. public class BankAccountTest {
  10. @Rule
  11. public ExpectedException expectedException = ExpectedException.none();
  12. private BankAccount bankAccount;
  13. @Before
  14. public void setup() {
  15. bankAccount = new BankAccount();
  16. }
  17. @Test
  18. public void newlyOpenedAccountHasEmptyBalance() throws BankAccountActionInvalidException {
  19. bankAccount.open();
  20. assertEquals(0, bankAccount.getBalance());
  21. }
  22. @Ignore
  23. @Test
  24. public void canDepositMoney() throws BankAccountActionInvalidException {
  25. bankAccount.open();
  26. bankAccount.deposit(10);
  27. assertEquals(10, bankAccount.getBalance());
  28. }
  29. @Ignore
  30. @Test
  31. public void canDepositMoneySequentially() throws BankAccountActionInvalidException {
  32. bankAccount.open();
  33. bankAccount.deposit(5);
  34. bankAccount.deposit(23);
  35. assertEquals(28, bankAccount.getBalance());
  36. }
  37. @Ignore
  38. @Test
  39. public void canWithdrawMoney() throws BankAccountActionInvalidException {
  40. bankAccount.open();
  41. bankAccount.deposit(10);
  42. bankAccount.withdraw(5);
  43. assertEquals(5, bankAccount.getBalance());
  44. }
  45. @Ignore
  46. @Test
  47. public void canWithdrawMoneySequentially() throws BankAccountActionInvalidException {
  48. bankAccount.open();
  49. bankAccount.deposit(23);
  50. bankAccount.withdraw(10);
  51. bankAccount.withdraw(13);
  52. assertEquals(0, bankAccount.getBalance());
  53. }
  54. @Ignore
  55. @Test
  56. public void cannotWithdrawMoneyFromEmptyAccount() throws BankAccountActionInvalidException {
  57. bankAccount.open();
  58. expectedException.expect(BankAccountActionInvalidException.class);
  59. expectedException.expectMessage("Cannot withdraw money from an empty account");
  60. bankAccount.withdraw(5);
  61. }
  62. @Ignore
  63. @Test
  64. public void cannotWithdrawMoreMoneyThanYouHave() throws BankAccountActionInvalidException {
  65. bankAccount.open();
  66. bankAccount.deposit(6);
  67. expectedException.expect(BankAccountActionInvalidException.class);
  68. expectedException.expectMessage("Cannot withdraw more money than is currently in the account");
  69. bankAccount.withdraw(7);
  70. }
  71. @Ignore
  72. @Test
  73. public void cannotDepositNegativeAmount() throws BankAccountActionInvalidException {
  74. bankAccount.open();
  75. expectedException.expect(BankAccountActionInvalidException.class);
  76. expectedException.expectMessage("Cannot deposit or withdraw negative amount");
  77. bankAccount.deposit(-1);
  78. }
  79. @Ignore
  80. @Test
  81. public void cannotWithdrawNegativeAmount() throws BankAccountActionInvalidException {
  82. bankAccount.open();
  83. bankAccount.deposit(105);
  84. expectedException.expect(BankAccountActionInvalidException.class);
  85. expectedException.expectMessage("Cannot deposit or withdraw negative amount");
  86. bankAccount.withdraw(-5);
  87. }
  88. @Ignore
  89. @Test
  90. public void cannotGetBalanceOfClosedAccount() throws BankAccountActionInvalidException {
  91. bankAccount.open();
  92. bankAccount.deposit(10);
  93. bankAccount.close();
  94. expectedException.expect(BankAccountActionInvalidException.class);
  95. expectedException.expectMessage("Account closed");
  96. bankAccount.getBalance();
  97. }
  98. @Ignore
  99. @Test
  100. public void cannotDepositMoneyIntoClosedAccount() throws BankAccountActionInvalidException {
  101. bankAccount.open();
  102. bankAccount.close();
  103. expectedException.expect(BankAccountActionInvalidException.class);
  104. expectedException.expectMessage("Account closed");
  105. bankAccount.deposit(5);
  106. }
  107. @Ignore
  108. @Test
  109. public void cannotWithdrawMoneyFromClosedAccount() throws BankAccountActionInvalidException {
  110. bankAccount.open();
  111. bankAccount.deposit(20);
  112. bankAccount.close();
  113. expectedException.expect(BankAccountActionInvalidException.class);
  114. expectedException.expectMessage("Account closed");
  115. bankAccount.withdraw(5);
  116. }
  117. @Ignore
  118. @Test
  119. public void bankAccountIsClosedBeforeItIsOpened() throws BankAccountActionInvalidException {
  120. expectedException.expect(BankAccountActionInvalidException.class);
  121. expectedException.expectMessage("Account closed");
  122. bankAccount.getBalance();
  123. }
  124. @Ignore
  125. @Test
  126. public void canAdjustBalanceConcurrently() throws BankAccountActionInvalidException, InterruptedException {
  127. bankAccount.open();
  128. bankAccount.deposit(1000);
  129. for (int i = 0; i < 1000; i++) {
  130. adjustBalanceConcurrently();
  131. }
  132. }
  133. private void adjustBalanceConcurrently() throws BankAccountActionInvalidException, InterruptedException {
  134. Random random = new Random();
  135. Thread[] threads = new Thread[1000];
  136. for (int i = 0; i < 1000; i++) {
  137. threads[i] = new Thread(() -> {
  138. try {
  139. bankAccount.deposit(5);
  140. Thread.sleep(random.nextInt(10));
  141. bankAccount.withdraw(5);
  142. } catch (BankAccountActionInvalidException e) {
  143. fail("Exception should not be thrown: " + e.getMessage());
  144. } catch (InterruptedException ignored) {
  145. }
  146. });
  147. threads[i].start();
  148. }
  149. for (Thread thread : threads) {
  150. thread.join();
  151. }
  152. assertEquals(1000, bankAccount.getBalance());
  153. }
  154. }