Site icon TestingDocs.com

Bank Account JUnit Tests

Overview

In this tutorial, we will learn how to develop JUnit tests for the bank account. Most of the tutorial is listing out the code and some sample tests on the concrete class SavingsAccount. In case you need more explanation, please comment below in the comments section.

Consider the following classes

Account class

package bankaccount;

public abstract class Account {
  private String name;	
  private int accountNumber;
  private double balance;

  public Account(String name,int accountNumber, double initBalance){
    this.name=name;
    this.accountNumber = accountNumber;
    this.balance = initBalance;
  }
  
  //no-arg constructor
  public Account(){
    
  }

  public String getName() {
    return name;
  }

  public void setName(String str) {
    this.name = str;
  }

  public int getAccountNumber() {
    return accountNumber;
  }
  
  public double getBalance() {
    return balance;
  }

  public void setBalance(double newBalance) {
    this.balance = newBalance;
  }
  
  public void setAccountNumber(int number) {
    this.accountNumber = number;
  }
}

 

Interface IAccount

package bankaccount;

//interface
public interface IAccount {
  public void withdraw(double amount);
  public void deposit(double amount);
  public int getAccountNumber();
  public double getBalance();
  public String getName();
}

 

SavingsAccount

package bankaccount;

public class SavingsAccount extends Account implements IAccount {
  private final double INTERESTRATE = 0.15;

  public SavingsAccount(String name,int accountNumber, double initBalance) {
    super(name,accountNumber, initBalance);
  }
  
  public SavingsAccount() {

  }

  @Override
  public void withdraw(double amount) {
    double newBalance = getBalance() - amount;
    if(newBalance<0){
      System.out.println("Withdraw not permitted");
      return;
    }else{
      System.out.println("Amount "+amount+"withdrawn successfully");
    }
    setBalance(newBalance);
  }

  @Override
  public void deposit(double amount) {
    System.out.println("Amount "+amount+" deposited successfully");
    double newBalance = getBalance() + amount;
    setBalance(newBalance);
  }

  @Override
  public int getAccountNumber() {
    return super.getAccountNumber();
  }

  @Override
  public double getBalance() {
    return super.getBalance();
  }

  @Override
  public String getName() {
    return super.getName();
  }

  public double calculateInterest() {
    if(getBalance() > 0 ) {
    return getBalance()*INTERESTRATE;
    }
    else {
      return 0.0;
    }
  }

}

Creating a JUnit 5 Test

Right-click on the Java class New >> Junit Test case . Choose a Test class name and select the methods.

 

 

JUnit 5 Tests

package bankaccount;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class SavingsAccountTest {
  SavingsAccount account;
  double delta=0.001;
  
  @BeforeEach
  void setUp() throws Exception {
    account= new SavingsAccount();
    account.setName("John");
    account.setBalance(5000);
    account.setAccountNumber(1234);
  }

  @AfterEach
  void tearDown() throws Exception {
    account=null;
  }

  @Test
  void depositTest() {
    account.deposit(500);
    assertEquals(account.getBalance(),5500,delta,"Deposit Test");
  }
  
  @Test
  void withdrawTest() {
    account.withdraw(500);
    assertEquals(account.getBalance(),4500.0,delta,"Withdraw Test");
  }
  
  @Test
  void interestTest() {
    double interest = account.calculateInterest();
    assertEquals(interest,750.0,delta,"Interest Test");
  }

}

 

All the tests should pass and the green color in the test runner window as shown below.

SavingsAccount Tests in Eclipse

Useful Link

Testing Calculator class with JUnit framework:

Exit mobile version