class Account { private int balance = 0; void deposit(int amount){ balance += amount; } void withdraw(int amount) throws IllegalArgumentException{ if (amount < 0) { throw new IllegalArgumentException("You cannot withdraw negative money!"); } if(amount > balance) { throw new IllegalArgumentException("You cannot withdraw more than you have!"); } balance -= amount ; } String getStatement() { return String.valueOf(balance); } }
- class Account {
- private int balance = 0;
- void deposit(int amount){
- balance += amount;
- }
- void withdraw(int amount) throws IllegalArgumentException{
- if (amount < 0) {
- throw new IllegalArgumentException("You cannot withdraw negative money!");
- }
- if(amount > balance) {
- throw new IllegalArgumentException("You cannot withdraw more than you have!");
- }
- balance -= amount ;
- }
- String getStatement() {
- return String.valueOf(balance);
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; class AccountTest { @Test void deposit_adds_money_to_initial_balance() { Account account = new Account(); account.deposit(100); String statement = account.getStatement(); assertEquals("100", statement); } @Test void deposit_adds_different_amount_of_money_to_initial_balance(){ Account account = new Account(); account.deposit(200); String statement = account.getStatement(); assertEquals("200", statement); } @Test void withdraw_of_zero_balance_remains_same(){ Account account = new Account(); account.withdraw(0); String statement = account.getStatement(); assertEquals("0", statement); } @Test void withdraw_reduces_balance_by_amount_withdrawn(){ Account account = new Account(); account.deposit(500); account.withdraw(100); String statement = account.getStatement(); assertEquals("400", statement); } @Test void cannot_withdraw_more_than_you_have() { Account account = new Account(); account.deposit(100); assertThrows(IllegalArgumentException.class, () -> { account.withdraw(200); }); } @Test void cannot_withdraw_negative_money() { Account account = new Account(); assertThrows(IllegalArgumentException.class, () -> { account.withdraw(-100); }); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.jupiter.api.Assertions.assertThrows;
- class AccountTest {
- @Test
- void deposit_adds_money_to_initial_balance() {
- Account account = new Account();
- account.deposit(100);
- String statement = account.getStatement();
- assertEquals("100", statement);
- }
- @Test
- void deposit_adds_different_amount_of_money_to_initial_balance(){
- Account account = new Account();
- account.deposit(200);
- String statement = account.getStatement();
- assertEquals("200", statement);
- }
- @Test
- void withdraw_of_zero_balance_remains_same(){
- Account account = new Account();
- account.withdraw(0);
- String statement = account.getStatement();
- assertEquals("0", statement);
- }
- @Test
- void withdraw_reduces_balance_by_amount_withdrawn(){
- Account account = new Account();
- account.deposit(500);
- account.withdraw(100);
- String statement = account.getStatement();
- assertEquals("400", statement);
- }
- @Test
- void cannot_withdraw_more_than_you_have() {
- Account account = new Account();
- account.deposit(100);
- assertThrows(IllegalArgumentException.class, () -> { account.withdraw(200); });
- }
- @Test
- void cannot_withdraw_negative_money() {
- Account account = new Account();
- assertThrows(IllegalArgumentException.class, () -> { account.withdraw(-100); });
- }
- }