Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions exercises/practice/bank-account/src/test/java/BankAccountTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Random;
Expand All @@ -14,54 +15,55 @@ public void setUp() {
bankAccount = new BankAccount();
}

@DisplayName("Newly opened account has empty balance")
@Test
public void newlyOpenedAccountHasEmptyBalance() throws BankAccountActionInvalidException {
bankAccount.open();

assertThat(bankAccount.getBalance()).isEqualTo(0);
}

@Disabled("Remove to run test")
@DisplayName("Deposit into account")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've opened issue #2971 with all the details.

As per the discussion there, the DisplayNames should be aligned exactly with the canonical-data.json in the problem-specifications repo, please make sure to follow that!

@Test
public void singleDeposit() throws BankAccountActionInvalidException {
bankAccount.open();
bankAccount.deposit(100);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’d be preferred if these empty lines were left as-is in each test, no need to remove them.

assertThat(bankAccount.getBalance()).isEqualTo(100);
}

@Disabled("Remove to run test")
@DisplayName("Deposit into account several times")
@Test
public void multipleDeposits() throws BankAccountActionInvalidException {
bankAccount.open();
bankAccount.deposit(100);
bankAccount.deposit(50);

assertThat(bankAccount.getBalance()).isEqualTo(150);
}

@Disabled("Remove to run test")
@DisplayName("Withdraw some money")
@Test
public void withdrawOnce() throws BankAccountActionInvalidException {
bankAccount.open();
bankAccount.deposit(100);
bankAccount.withdraw(75);

assertThat(bankAccount.getBalance()).isEqualTo(25);
}

@Disabled("Remove to run test")
@DisplayName("Withdraw entire balance")
@Test
public void withdrawTwice() throws BankAccountActionInvalidException {
bankAccount.open();
bankAccount.deposit(100);
bankAccount.withdraw(80);
bankAccount.withdraw(20);

assertThat(bankAccount.getBalance()).isEqualTo(0);
}

@Disabled("Remove to run test")
@DisplayName("Can do multiple operations sequentially")
@Test
public void canDoMultipleOperationsSequentially() throws BankAccountActionInvalidException {
bankAccount.open();
Expand All @@ -70,33 +72,33 @@ public void canDoMultipleOperationsSequentially() throws BankAccountActionInvali
bankAccount.withdraw(200);
bankAccount.deposit(60);
bankAccount.withdraw(50);

assertThat(bankAccount.getBalance()).isEqualTo(20);
}

@Disabled("Remove to run test")
@DisplayName("Cannot get balance of closed account")
@Test
public void cannotCheckBalanceOfClosedAccount() throws BankAccountActionInvalidException {
bankAccount.open();
bankAccount.close();

assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(bankAccount::getBalance)
.withMessage("Account closed");
}

@Disabled("Remove to run test")
@DisplayName("Cannot deposit money into closed account")
@Test
public void cannotDepositIntoClosedAccount() throws BankAccountActionInvalidException {
bankAccount.open();
bankAccount.close();

assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(() -> bankAccount.deposit(50))
.withMessage("Account closed");
}

@Disabled("Remove to run test")
@DisplayName("Cannot deposit money into unopened account")
@Test
public void cannotDepositIntoUnopenedAccount() {
assertThatExceptionOfType(BankAccountActionInvalidException.class)
Expand All @@ -105,17 +107,18 @@ public void cannotDepositIntoUnopenedAccount() {
}

@Disabled("Remove to run test")
@DisplayName("Cannot withdraw money from closed account")
@Test
public void cannotWithdrawFromClosedAccount() throws BankAccountActionInvalidException {
bankAccount.open();
bankAccount.close();

assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(() -> bankAccount.withdraw(50))
.withMessage("Account closed");
}

@Disabled("Remove to run test")
@DisplayName("Cannot close unopened account")
@Test
public void cannotCloseAnAccountThatWasNotOpened() {
assertThatExceptionOfType(BankAccountActionInvalidException.class)
Expand All @@ -124,59 +127,60 @@ public void cannotCloseAnAccountThatWasNotOpened() {
}

@Disabled("Remove to run test")
@DisplayName("Cannot open already opened account")
@Test
public void cannotOpenAnAlreadyOpenedAccount() throws BankAccountActionInvalidException {
bankAccount.open();

assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(bankAccount::open)
.withMessage("Account already open");
}

@Disabled("Remove to run test")
@DisplayName("Reopened account does not retain balance")
@Test
public void reopenedAccountDoesNotRetainBalance() throws BankAccountActionInvalidException {
bankAccount.open();
bankAccount.deposit(50);
bankAccount.close();
bankAccount.open();

assertThat(bankAccount.getBalance()).isEqualTo(0);
}

@Disabled("Remove to run test")
@DisplayName("Cannot withdraw more than was deposited")
@Test
public void cannotWithdrawMoreThanWasDeposited() throws BankAccountActionInvalidException {
bankAccount.open();
bankAccount.deposit(25);

assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(() -> bankAccount.withdraw(50))
.withMessage("Cannot withdraw more money than is currently in the account");
}

@Disabled("Remove to run test")
@DisplayName("Cannot withdraw negative amount")
@Test
public void cannotWithdrawNegativeAmount() throws BankAccountActionInvalidException {
bankAccount.open();
bankAccount.deposit(100);

assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(() -> bankAccount.withdraw(-50))
.withMessage("Cannot deposit or withdraw negative amount");
}

@Disabled("Remove to run test")
@DisplayName("Cannot deposit negative amount")
@Test
public void cannotDepositNegativeAmount() throws BankAccountActionInvalidException {
bankAccount.open();

assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(() -> bankAccount.deposit(-50))
.withMessage("Cannot deposit or withdraw negative amount");
}

@Disabled("Remove to run test")
@DisplayName("Can handle concurrent transactions")
@Test
public void canHandleConcurrentTransactions() throws BankAccountActionInvalidException, InterruptedException {
bankAccount.open();
Expand Down